Node-redis: [Question] Difference between HMSET and HSET implementation?

Created on 30 Jun 2020  路  1Comment  路  Source: NodeRedis/node-redis

Hello,

I have a doubt on the implementation of two related methods in the node-redis client. According to the documentation: https://redis.io/commands/hmset
'As per Redis 4.0.0, HMSET is considered deprecated. Please use HSET in new code.'

My confusion is that, when using

client.hmset(key, object)

I find no issues passing object as an object. But when using

client.hset(key, object)

the client warns about object being parsed as [Object object],

This is converted to "[object Object]" by using .toString() now and will return an error from v.3.0 on.

My question is, would the use of,

client.hset(key, JSON.stringify(object))

still work with client.hgetall in retrieving an object just as it does when the object was stored using client.hmset , or on the contrary will retrieve a string?

Most helpful comment

We should probably deprecate client.hmset here too. I advise against using it generally - using anything but key/value pairs will causes errors. E.g.:
client.hmset('bar', { one : 1, two: 2 }); works
but
client.hmset('bar', { one : { two: 2 }}); throws an error.

Objects are really the wrong type for going into a Redis Hash, so your solution of using JSON.stringify won't work (ERR wrong number of arguments for 'hset' command). client.hgetall will work with _any_ hash in Redis, so it doesn't matter how you put it in.

If you wanted to avoid using client.hmset due to deprecation, you can use this instead of JSON.stringify
client.hset('key',Object.entries({ one : 1, two: 2 }).flat())
Keep in mind that this will have the same nesting drawbacks as client.hmset.

>All comments

We should probably deprecate client.hmset here too. I advise against using it generally - using anything but key/value pairs will causes errors. E.g.:
client.hmset('bar', { one : 1, two: 2 }); works
but
client.hmset('bar', { one : { two: 2 }}); throws an error.

Objects are really the wrong type for going into a Redis Hash, so your solution of using JSON.stringify won't work (ERR wrong number of arguments for 'hset' command). client.hgetall will work with _any_ hash in Redis, so it doesn't matter how you put it in.

If you wanted to avoid using client.hmset due to deprecation, you can use this instead of JSON.stringify
client.hset('key',Object.entries({ one : 1, two: 2 }).flat())
Keep in mind that this will have the same nesting drawbacks as client.hmset.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dotSlashLu picture dotSlashLu  路  5Comments

juriansluiman picture juriansluiman  路  3Comments

Alchemystic picture Alchemystic  路  6Comments

Stono picture Stono  路  6Comments

michaelwittig picture michaelwittig  路  3Comments