我正在尝试将Redis数据库与我正在构建的Node.js应用程序连接起来,以便能够存储有关物品的评论。我使用node_redis库处理连接。然而,当我尝试从数据库中检索注释时,只返回“[true]”。为了测试目的,我将所有内容都放入一个方法中,并硬编码了值,但仍然收到“[true]”。
exports.getComment = function (id){
var comments = new Array();
rc.hmset("hosts", "mjr", "1", "another", "23", "home", "1234");
comments.push(rc.hgetall("hosts", function (err, obj) {
var comment = new Array();
if(err){
comment.push("Error");
} else {
comment.push(obj);
}
return comment;
}));
return comments;
}
根据教程更新代码,以下是结果:
获取评论:
exports.getComment = function (id, callback){
rc.hgetall(id, callback);
}
添加注释:
exports.addComment = function (id, area, content, author){
//add comment into the database
rc.hmset("comment",
"id", id,
"area", area,
"content", content,
"author" , author,
function(error, result) {
if (error) res.send('Error: ' + error);
});
//returns nothing
};
要呈现的代码:
var a = [];
require('../lib/annotations').addComment("comment");
require('../lib/annotations').getComment("comment", function(comment){
a.push(comment)
});
res.json(a);