【发布时间】:2016-05-19 03:25:27
【问题描述】:
我已经声明了一个查询 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;
}
}
})
});
【问题讨论】:
-
您可以在调用函数时而不是在声明函数时将函数作为参数。
-
基本上当你声明一个函数时,它应该看起来像这样
function redusId(taskId,callback){...},当你像这样调用它时redusId(1, function(){...}); -
@Molda 谢谢,它解决了这个问题。
-
@SaurabhAriyan 如果您在 cmets 中找到了答案,并且没有人发布该答案作为答案,那么在此发布您自己的答案并接受它是公平的游戏点,这样我们就可以结束 JSLint 标记上的问题。
标签: node.js jslint jshint redisclient