繁体   English   中英

Nodejs Redis HSET和HGET插入数据集错误

[英]Nodejs Redis HSET & HGET insert to dataset Error

我正在尝试使用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将所有内容存储为字符串(毕竟,该协议是基于文本的)。

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

尝试:

 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);     
 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM