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

标签 node.js redis hashset

我正在尝试使用 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/

相关文章:

javascript - 如何在 Mongoose 中使用 findByIdAndUpdate 填充数组

javascript - 如何删除shortId中的特殊字符

javascript - Mongoose QueryStream.pause() 不暂停?

mysql - (Neo4J 或 mysql)和 redis - 社交网络

redis - 每24小时增加关键天数

ruby - 我是否以正确的方式使用 eventmachine?

lambda - 使用方法引用与HashSet复制

node.js - 为什么负载均衡器后面的两台服务器都会响应每个 HTTP 请求?

java - 更改集合中的元素会更改 'equals' 语义

java - 具有两个相等对象的 HashSet?