admin管理员组文章数量:1336180
The Redis mand HMSET has been deprecated since version 4. They suggest using HSET instead. But when trying that I get a different deprecation warning.
I was using: db.hmset('key', {a: 1, b: 'c'})
. Now I tried to replace it with db.hset
but that triggers:
node_redis: Deprecated: The HSET mand contains a argument of type Object.
This is converted to "[object Object]" by using .toString() now and will return an error from v.3.0 on.
Please handle this in your code to make sure everything works as you intended it to.
What is the correct way to store an object in a Redis database?
The documentation for Redis HSET states: 'As of Redis 4.0.0, HSET is variadic and allows for multiple field/value pairs.' I want to store the whole object as it would be using hmset
in the database, not its string representation.
The Redis mand HMSET has been deprecated since version 4. They suggest using HSET instead. But when trying that I get a different deprecation warning.
I was using: db.hmset('key', {a: 1, b: 'c'})
. Now I tried to replace it with db.hset
but that triggers:
node_redis: Deprecated: The HSET mand contains a argument of type Object.
This is converted to "[object Object]" by using .toString() now and will return an error from v.3.0 on.
Please handle this in your code to make sure everything works as you intended it to.
What is the correct way to store an object in a Redis database?
The documentation for Redis HSET states: 'As of Redis 4.0.0, HSET is variadic and allows for multiple field/value pairs.' I want to store the whole object as it would be using hmset
in the database, not its string representation.
3 Answers
Reset to default 2as described here, redis client dose not support objects in a mand arguments.
To easily store an object in redis you can do:
db.hset('key', ...Object.entries({a: 'a', b: 'b'}), (err) => {
// ...
});
note that it'll ignore Symbol
keys, and work only with "flat objects".
For those using Typescript, the second argument of hSet can be an array of strings of this kind ['k1', 'v1', 'k2', 'v2',...]. So the following code works:
const obj: Record<string, string | number> = {a: 'a', n: 1};
await client.hSet('key', [...Object.entries(obj).flat()]);
Well it seems that the second argument of hset
must be a string. If it's not .toString
will be applied to it. But as it will return "[object Object]"
in your case, the warning triggers. Maybe use JSON.stringify({a: 1, b: 'c'})
as parameter instead
本文标签: javascriptHow to replace deprecated HMSET in in nodejs REDISStack Overflow
版权声明:本文标题:javascript - How to replace deprecated HMSET in in node.js REDIS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742398527a2467410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论