2015-10-19 34 views
0

我想插入註釋在redis中用HSET作爲首選選項,但出現錯誤。 下面是代碼:Nodejs Redis HSET&HGET插入到數據集錯誤

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或置角,其中之一應使用之間的無能。

+0

看起來你需要HMSET - 的「M」是倍數,如在哈希設置多個領域。這就像爲不同的領域多次做HMSET一樣。 –

+0

確實插入解決方案resame,我必須以stringify格式存儲所有內容?如下所示: redisClient.hmset('comment',JSON.stringify(commmentData),function(err,reply){if }); –

回答

0

Redis以字符串形式存儲所有內容(畢竟,協議是基於文本的)。

如果要在Redis中存儲對象,請確保在保存和反序列化之前(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);  
}); 
+0

如果我們將所有內容都存儲在字符串中,那麼將如何進行搜索。 HGET或HMGET是否使用任何搜索索引參數。 例如,如果我們想用userId或日期進行搜索。 –

+0

我已經嘗試了以上解決方案,但仍然收到錯誤: c:\ Users \ ACER \ Documents \ GitHub \ api \ routes \ comments.js:74 if(err)throw err; ^ 錯誤:'hset'命令的參數ERR數量錯誤 –

+0

@ShubhoPramanik它需要4個參數 – Suvitruf