redis - 如何在redis中将对象数组保存为哈希?
问题描述
我正在使用redis
将用户信息保存在 nodejs 项目中。用户具有以下结构:
const user = {
name: 'u1',
age: 20,
fav: [{x:0, y:0}, {x:1, y:1}]
}
当我通过它将这些数据保存到redis中时,node-redis
它会给我这个警告信息:
client.hmset(userId, user, err => {
...
}
node_redis: Deprecated: The HMSET command contains a argument of type Array.
This is converted to "[object Object],[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.
redis上保存的数据是:
127.0.0.1:6379[2]> HGETALL 0
1) "name"
2) "u1"
3) "age"
4) "20"
5) "fav"
6) "[object Object],[object Object]"
我想知道在redis中保存对象数组的最佳方法是什么?
解决方案
遇到同样的问题后,我相信保存数据最直接的解决方案是:
client.hmset(userId, JSON.stringify(user), err => {...})
当您检索数据时,您只需要在询问 userId 数据后:
JSON.parse(user)
推荐阅读
- swift - apple-app-site-association 文件是否适用于 GitHub Pages(即 site.github.io)?
- reactjs - 为什么我的日历小部件不允许我选择原始开始日之后的天数?
- python - turtle.Screen().screensize() 没有输出正确的屏幕尺寸
- c++ - 将地址分配给本地指针后,公共对象的变量消失
- javascript - Redux reducer 不显示我的控制台日志并返回 undefined
- php - 如何从 XML 中隐藏特定元素?
- javascript - 错误 [object Object] - 通过 File API 上传 Firebase 存储文件
- excel - 用大写字母更改单词的颜色
- python - 列表到字典:有重复的元素将充当键,但我想针对同一个键收集值并将其转换为列表值
- javascript - 使用本地存储和 Jquery 添加/删除收藏夹多个产品