如何在redis中将对象数组保存为哈希?

时间:2018-05-28 01:01:59

标签: redis node-redis

我正在使用redis在nodejs项目中保存用户信息。用户具有以下结构:

const user = {
    name: 'u1',
    age: 20,
    fav: [{x:0, y:0}, {x:1, y:1}]
}

当我通过node-redis将此数据保存到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中保存对象数组的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

面对相同的问题后,我认为保存数据的最直接的方法是:

xlsxfile.list <- list.files(path = path, pattern='*.xlsx', full.names = TRUE)
filePath  <- list.files(path=path,recursive=T,pattern=".xlsx",full.names=T)

当您检索数据时,只需要在询问userId数据后即可:

client.hmset(userId, JSON.stringify(user), err => {...})