我已经声明了一个查询Redis的函数,如果在redis中找不到数据,则查询SQL数据库。以下是我的代码,它在第一行本身给出了错误。我不确定为什么作为参数的函数会导致麻烦。
function redusId(taskId, function (err, reply){
var status = taskId + ".status";
var response = taskId + ".response";
var jsonObject = {};
redisClient.get(status, function (error, reply) {
if (error) {
console.log("redis.status.ERROR: " + error);
return;
}
if (!reply) {
checkSQLdb(taskId, function (error, data) {
if(error) {
console.log("sql.ERROR", error);
}
if(!data) {
console.log("sql.status.ERROR");
}
else {
// data retrieval and posting in redis and calling redis client again
var id = data[0].id;
var status = data[0].status;
var response = data[0].response;
console.log(id, status, response);
var requestid = id + ".status";
redisClient.set(requestid, status);
requestid = id + ".response";
redisClient.set(requestid, response);
redusId(id);
}
})
}
else {
jsonObject."status" = reply;
if (reply == 1) {
//redis returns non one status no response is expected
redisClient.get(response, function(error, reply) {
if (error) {
//redis has the status but not the response
console.log("redis.response.ERROR ", error);
}
else {
jsonObject."response" = response;
return jsonObject;
}
});
}
else {
console.log("status is not one ; no response is expected");
return jsonObject;
}
}
})
});
答案 0 :(得分:0)
这在评论中已经指出,现在没有人张贴作为答案因此发布。
代码片段中的问题是我试图声明该函数并将其作为参数传递。在给定的代码段中,正确的方法
function redusId(taskId,callback){...}
然后将其称为
redusId(1, function(){...});