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?
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
.
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 });
worksbut
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 ofJSON.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
.