我正在尝试将 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);