我正试图将一个Redis数据库与一个Node.js应用程序连接起来,以便能够存储有关项目的注释。我正在使用node_redis库来处理连接。当我尝试从数据库中检索注释时,只返回“[true]”。出于测试的目的,我将所有东西都塞进了一个方法中,并且我已经对这些值进行了硬编码,但我仍然收到“[true]”。Node.js和Redis
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);
克里斯Maness直言不讳:请更新您的问题,不是我的答案:) –