gpt4 book ai didi

node.js - Nodejs Redis HSET & HGET 插入到数据集错误

转载 作者:可可西里 更新时间:2023-11-01 11:36:15 24 4
gpt4 key购买 nike

我正在尝试使用 HSET 作为首选选项在 Redis 中为帖子插入评论,但出现错误。下面是代码:

 var commmentData ={
id : id,
comment : req.body.comment,
postId : req.body.postId,
userId : req.body.userId
}
redisClient.hset('comment', commmentData, function(err, reply) {
if (err) throw err;
console.log("Reply : "+ reply);
res.json(errorResponse.res.SaveSuccess);
});

HMSET 和 HSET 到底该用哪个,我还是一头雾水。

最佳答案

Redis 将所有内容存储为字符串(毕竟该协议(protocol)是基于文本的)。

如果要在 Redis 中存储对象,请确保在保存和反序列化 (JSON.parse()) 之前对它们进行序列化 (JSON.stringify()) >) 检索后。

尝试:

 var commmentData ={
id : id,
comment : req.body.comment,
postId : req.body.postId,
userId : req.body.userId
}

redisClient.hset('comment', id, JSON.stringify(commmentData), function(err, reply) {
if (err) throw err;

console.log("Reply : "+ reply);
});

关于node.js - Nodejs Redis HSET & HGET 插入到数据集错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33217688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com