Question

I try to use redis sorted set command zadd. But keeping throw error when i run this script:

var ts = Math.round(Date.now() / 1000)
      , key = 'usr::' + dest.ID + '::msgs'
      , id = uuid.v1();
var notify = {
    msg: response.msg,
    from: response.from ? response.from : null,
    type: response.type ? response.type : null,
    date: ts,
    read: 0
}
client.zadd(key, ts, JSON.stringify(notify), function (err, response) {
    if (err) throw err;
});

Is anything wrong with this code?

By the way: What i try to accomplish is notification/inbox system... So better save time from asking me like you will help and finally you don't :(

ERROR: ERR Operation against a key holding the wrong kind of value

Était-ce utile?

La solution

I would say the key already exists in Redis and is not a sorted set. Try to see if you already have usr::ID::msgs entries in Redis, and check their type.

Update:

If you keep just one sorted set, it is not really possible to update an entry, because the entry data are serialized and used as the value of the sorted set item.

You have several solutions though:

1) you can read and remove the item, deserialize, change the read status, serialize again, add the item in the sorted set again. It can be done with one roundtrip by using a server-side Lua script if needed.

2) you can split your data model in multiple objects: keep one sorted set associating timestamp and message id, and use one hash object per message id to store the properties of each message. Updating the read status of the message is therefore easy (HMSET).

3) you can have also two sorted sets (one for read messages, one for unread messages). Changing the status of the message would involve removing the item from one set, and add it in the other one.

The best choice for the solution probably depends on your data access patterns.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top