Javascript Express Node.JS-接收Redis回调,执行承诺
我有一个Node/Express路由函数,它在另一个模块中执行Redis调用。我想在一个节点模块中执行一个复杂的Redis函数,并发送一个简单的回调,告诉路由模块这是成功的。Redis调用将执行,但我无法执行任何同步函数,即使从Redis调用中检索一个简单的真值。以下是我的Redis功能: doctorDB.jsJavascript Express Node.JS-接收Redis回调,执行承诺,javascript,node.js,express,redis,node-redis,Javascript,Node.js,Express,Redis,Node Redis,我有一个Node/Express路由函数,它在另一个模块中执行Redis调用。我想在一个节点模块中执行一个复杂的Redis函数,并发送一个简单的回调,告诉路由模块这是成功的。Redis调用将执行,但我无法执行任何同步函数,即使从Redis调用中检索一个简单的真值。以下是我的Redis功能: doctorDB.js var addDoctor = function addDoctor(id, doc){ var fields = Object.keys(doc.fields);
var addDoctor = function addDoctor(id, doc){
var fields = Object.keys(doc.fields);
return client.multi()
.sadd("Doctors", id)
.hmset(id, "lastName", doc.lastName, "firstName", doc.firstName)
.hmset(id, "email", doc.email, "university", doc.university, "work", doc.work)
.sadd(id + ":fields", fields)
.exec(function(err, replies){
console.log("It's in this");
if (doc.middleName){
console.log("Now here");
client.hset(id, "middleName", doc.middleName);
return true;
} else {
console.log("Or here");
return true;
}
});
};
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
// function getThis(req){
// var deferred = Q.defer();
// doctorDB.addDoctor(req.body.id, req.body.doc).then(function(response){
// deferred.resolve(response);
// }, function(err){
// console.log(err);
// return deferred.resolve(err);
// });
// return deferred.promise;
// }
// var x = getThis(req);
// console.log(x);
doctorDB.addDoctor(req.body.id, req.body.doc).then(function(x){
console.log(x);
}).catch(function(err){
console.log(err);
}).finally(function(){
console.log("We made it!");
});
// function sendMsg(info){
// console.log(info);
// res.send({success: true});
// }
// async.waterfall([
// doctorDB.addDoctor(req.body.id, req.body.doc),
// sendMsg(info)
// ], function(err){
// console.log(err)
// });
// var DBCALL = doctorDB.addDoctor(req.body.id, req.body.doc);
// Q.fcall(DBCALL).then(function(x){
// return console.log(x);
// }).catch(function(err){
// console.log(err);
// });
});
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
return doctorDB.addDoctor(req.body.id, req.body.doc, req, res, next);
});
var addDoctor = function addDoctor(id, doc, req, res, next){
var fields = Object.keys(doc.fields);
function middleName(id, doc){
if (doc.middleName){ return client.hset(id, "middleName", doc.middleName); }
else { return; }
}
return Q.all([Q.ninvoke(client, 'sadd', 'Doctors', id),
Q.ninvoke(client, 'hmset', id, "lastName", doc.lastName, "firstName", doc.firstName, "email", doc.email, "university", doc.university, "work", doc.work),
Q.ninvoke(client, 'sadd', id + ':fields', fields),
middleName(id, doc)]).then(function(x){
return getInfo(id, req, res, next);;
}, function (err) { res.status(404); });
};
一切都在为此而努力。现在,我希望将回调发送到Express路由器,以便向客户端发送响应。我希望它是一个同步函数的形式,我已经尝试了很多使用Q和Async的方法,但是没有任何效果。所以要么A.我没有完全掌握承诺函数,要么B.我没有完全掌握将值返回到另一个模块。任何帮助都将不胜感激
以下是express router端的许多失败尝试,仅供参考:
routes.js
var addDoctor = function addDoctor(id, doc){
var fields = Object.keys(doc.fields);
return client.multi()
.sadd("Doctors", id)
.hmset(id, "lastName", doc.lastName, "firstName", doc.firstName)
.hmset(id, "email", doc.email, "university", doc.university, "work", doc.work)
.sadd(id + ":fields", fields)
.exec(function(err, replies){
console.log("It's in this");
if (doc.middleName){
console.log("Now here");
client.hset(id, "middleName", doc.middleName);
return true;
} else {
console.log("Or here");
return true;
}
});
};
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
// function getThis(req){
// var deferred = Q.defer();
// doctorDB.addDoctor(req.body.id, req.body.doc).then(function(response){
// deferred.resolve(response);
// }, function(err){
// console.log(err);
// return deferred.resolve(err);
// });
// return deferred.promise;
// }
// var x = getThis(req);
// console.log(x);
doctorDB.addDoctor(req.body.id, req.body.doc).then(function(x){
console.log(x);
}).catch(function(err){
console.log(err);
}).finally(function(){
console.log("We made it!");
});
// function sendMsg(info){
// console.log(info);
// res.send({success: true});
// }
// async.waterfall([
// doctorDB.addDoctor(req.body.id, req.body.doc),
// sendMsg(info)
// ], function(err){
// console.log(err)
// });
// var DBCALL = doctorDB.addDoctor(req.body.id, req.body.doc);
// Q.fcall(DBCALL).then(function(x){
// return console.log(x);
// }).catch(function(err){
// console.log(err);
// });
});
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
return doctorDB.addDoctor(req.body.id, req.body.doc, req, res, next);
});
var addDoctor = function addDoctor(id, doc, req, res, next){
var fields = Object.keys(doc.fields);
function middleName(id, doc){
if (doc.middleName){ return client.hset(id, "middleName", doc.middleName); }
else { return; }
}
return Q.all([Q.ninvoke(client, 'sadd', 'Doctors', id),
Q.ninvoke(client, 'hmset', id, "lastName", doc.lastName, "firstName", doc.firstName, "email", doc.email, "university", doc.university, "work", doc.work),
Q.ninvoke(client, 'sadd', id + ':fields', fields),
middleName(id, doc)]).then(function(x){
return getInfo(id, req, res, next);;
}, function (err) { res.status(404); });
};
我明白了。我使用Q库来执行所有函数,而不是client.multi().exec()。这允许干净地执行所有redispost命令,然后允许我检索信息 在routes.js文件中,我只有一小段代码。一切都在doctorDB.js文件中执行 routes.js
var addDoctor = function addDoctor(id, doc){
var fields = Object.keys(doc.fields);
return client.multi()
.sadd("Doctors", id)
.hmset(id, "lastName", doc.lastName, "firstName", doc.firstName)
.hmset(id, "email", doc.email, "university", doc.university, "work", doc.work)
.sadd(id + ":fields", fields)
.exec(function(err, replies){
console.log("It's in this");
if (doc.middleName){
console.log("Now here");
client.hset(id, "middleName", doc.middleName);
return true;
} else {
console.log("Or here");
return true;
}
});
};
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
// function getThis(req){
// var deferred = Q.defer();
// doctorDB.addDoctor(req.body.id, req.body.doc).then(function(response){
// deferred.resolve(response);
// }, function(err){
// console.log(err);
// return deferred.resolve(err);
// });
// return deferred.promise;
// }
// var x = getThis(req);
// console.log(x);
doctorDB.addDoctor(req.body.id, req.body.doc).then(function(x){
console.log(x);
}).catch(function(err){
console.log(err);
}).finally(function(){
console.log("We made it!");
});
// function sendMsg(info){
// console.log(info);
// res.send({success: true});
// }
// async.waterfall([
// doctorDB.addDoctor(req.body.id, req.body.doc),
// sendMsg(info)
// ], function(err){
// console.log(err)
// });
// var DBCALL = doctorDB.addDoctor(req.body.id, req.body.doc);
// Q.fcall(DBCALL).then(function(x){
// return console.log(x);
// }).catch(function(err){
// console.log(err);
// });
});
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
return doctorDB.addDoctor(req.body.id, req.body.doc, req, res, next);
});
var addDoctor = function addDoctor(id, doc, req, res, next){
var fields = Object.keys(doc.fields);
function middleName(id, doc){
if (doc.middleName){ return client.hset(id, "middleName", doc.middleName); }
else { return; }
}
return Q.all([Q.ninvoke(client, 'sadd', 'Doctors', id),
Q.ninvoke(client, 'hmset', id, "lastName", doc.lastName, "firstName", doc.firstName, "email", doc.email, "university", doc.university, "work", doc.work),
Q.ninvoke(client, 'sadd', id + ':fields', fields),
middleName(id, doc)]).then(function(x){
return getInfo(id, req, res, next);;
}, function (err) { res.status(404); });
};
doctorDB.js
var addDoctor = function addDoctor(id, doc){
var fields = Object.keys(doc.fields);
return client.multi()
.sadd("Doctors", id)
.hmset(id, "lastName", doc.lastName, "firstName", doc.firstName)
.hmset(id, "email", doc.email, "university", doc.university, "work", doc.work)
.sadd(id + ":fields", fields)
.exec(function(err, replies){
console.log("It's in this");
if (doc.middleName){
console.log("Now here");
client.hset(id, "middleName", doc.middleName);
return true;
} else {
console.log("Or here");
return true;
}
});
};
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
// function getThis(req){
// var deferred = Q.defer();
// doctorDB.addDoctor(req.body.id, req.body.doc).then(function(response){
// deferred.resolve(response);
// }, function(err){
// console.log(err);
// return deferred.resolve(err);
// });
// return deferred.promise;
// }
// var x = getThis(req);
// console.log(x);
doctorDB.addDoctor(req.body.id, req.body.doc).then(function(x){
console.log(x);
}).catch(function(err){
console.log(err);
}).finally(function(){
console.log("We made it!");
});
// function sendMsg(info){
// console.log(info);
// res.send({success: true});
// }
// async.waterfall([
// doctorDB.addDoctor(req.body.id, req.body.doc),
// sendMsg(info)
// ], function(err){
// console.log(err)
// });
// var DBCALL = doctorDB.addDoctor(req.body.id, req.body.doc);
// Q.fcall(DBCALL).then(function(x){
// return console.log(x);
// }).catch(function(err){
// console.log(err);
// });
});
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
return doctorDB.addDoctor(req.body.id, req.body.doc, req, res, next);
});
var addDoctor = function addDoctor(id, doc, req, res, next){
var fields = Object.keys(doc.fields);
function middleName(id, doc){
if (doc.middleName){ return client.hset(id, "middleName", doc.middleName); }
else { return; }
}
return Q.all([Q.ninvoke(client, 'sadd', 'Doctors', id),
Q.ninvoke(client, 'hmset', id, "lastName", doc.lastName, "firstName", doc.firstName, "email", doc.email, "university", doc.university, "work", doc.work),
Q.ninvoke(client, 'sadd', id + ':fields', fields),
middleName(id, doc)]).then(function(x){
return getInfo(id, req, res, next);;
}, function (err) { res.status(404); });
};
这将传递给函数getInfo(),该函数将向客户端发送响应:
var redisHGetAll = Q.nbind(client.hgetall, client);
var getInfo = function getInfo(id, req, res, next){
return redisHGetAll(id).then(function(x){
return findByMatchingProperties(x);
}, function (err) { res.status(404); }).then(function(){
return client.smembers(id + ':fields', function(err, reply){
data['fields'] = reply;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(data));
});
}, function (err) { res.status(404); })
};
function findByMatchingProperties(x) {
for (var y in x){
checkData(y, x[y]);
}
function checkData(y, z){
for (var d in data){
if (d === y){
data[d] = z;
}
}
}
}
var data = {
lastName: null,
firstName: null,
middleName: null,
email: null,
university: null,
work: null,
fields: null
};
我明白了。我使用Q库来执行所有函数,而不是client.multi().exec()。这允许干净地执行所有redispost命令,然后允许我检索信息 在routes.js文件中,我只有一小段代码。一切都在doctorDB.js文件中执行 routes.js
var addDoctor = function addDoctor(id, doc){
var fields = Object.keys(doc.fields);
return client.multi()
.sadd("Doctors", id)
.hmset(id, "lastName", doc.lastName, "firstName", doc.firstName)
.hmset(id, "email", doc.email, "university", doc.university, "work", doc.work)
.sadd(id + ":fields", fields)
.exec(function(err, replies){
console.log("It's in this");
if (doc.middleName){
console.log("Now here");
client.hset(id, "middleName", doc.middleName);
return true;
} else {
console.log("Or here");
return true;
}
});
};
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
// function getThis(req){
// var deferred = Q.defer();
// doctorDB.addDoctor(req.body.id, req.body.doc).then(function(response){
// deferred.resolve(response);
// }, function(err){
// console.log(err);
// return deferred.resolve(err);
// });
// return deferred.promise;
// }
// var x = getThis(req);
// console.log(x);
doctorDB.addDoctor(req.body.id, req.body.doc).then(function(x){
console.log(x);
}).catch(function(err){
console.log(err);
}).finally(function(){
console.log("We made it!");
});
// function sendMsg(info){
// console.log(info);
// res.send({success: true});
// }
// async.waterfall([
// doctorDB.addDoctor(req.body.id, req.body.doc),
// sendMsg(info)
// ], function(err){
// console.log(err)
// });
// var DBCALL = doctorDB.addDoctor(req.body.id, req.body.doc);
// Q.fcall(DBCALL).then(function(x){
// return console.log(x);
// }).catch(function(err){
// console.log(err);
// });
});
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
return doctorDB.addDoctor(req.body.id, req.body.doc, req, res, next);
});
var addDoctor = function addDoctor(id, doc, req, res, next){
var fields = Object.keys(doc.fields);
function middleName(id, doc){
if (doc.middleName){ return client.hset(id, "middleName", doc.middleName); }
else { return; }
}
return Q.all([Q.ninvoke(client, 'sadd', 'Doctors', id),
Q.ninvoke(client, 'hmset', id, "lastName", doc.lastName, "firstName", doc.firstName, "email", doc.email, "university", doc.university, "work", doc.work),
Q.ninvoke(client, 'sadd', id + ':fields', fields),
middleName(id, doc)]).then(function(x){
return getInfo(id, req, res, next);;
}, function (err) { res.status(404); });
};
doctorDB.js
var addDoctor = function addDoctor(id, doc){
var fields = Object.keys(doc.fields);
return client.multi()
.sadd("Doctors", id)
.hmset(id, "lastName", doc.lastName, "firstName", doc.firstName)
.hmset(id, "email", doc.email, "university", doc.university, "work", doc.work)
.sadd(id + ":fields", fields)
.exec(function(err, replies){
console.log("It's in this");
if (doc.middleName){
console.log("Now here");
client.hset(id, "middleName", doc.middleName);
return true;
} else {
console.log("Or here");
return true;
}
});
};
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
// function getThis(req){
// var deferred = Q.defer();
// doctorDB.addDoctor(req.body.id, req.body.doc).then(function(response){
// deferred.resolve(response);
// }, function(err){
// console.log(err);
// return deferred.resolve(err);
// });
// return deferred.promise;
// }
// var x = getThis(req);
// console.log(x);
doctorDB.addDoctor(req.body.id, req.body.doc).then(function(x){
console.log(x);
}).catch(function(err){
console.log(err);
}).finally(function(){
console.log("We made it!");
});
// function sendMsg(info){
// console.log(info);
// res.send({success: true});
// }
// async.waterfall([
// doctorDB.addDoctor(req.body.id, req.body.doc),
// sendMsg(info)
// ], function(err){
// console.log(err)
// });
// var DBCALL = doctorDB.addDoctor(req.body.id, req.body.doc);
// Q.fcall(DBCALL).then(function(x){
// return console.log(x);
// }).catch(function(err){
// console.log(err);
// });
});
app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
return doctorDB.addDoctor(req.body.id, req.body.doc, req, res, next);
});
var addDoctor = function addDoctor(id, doc, req, res, next){
var fields = Object.keys(doc.fields);
function middleName(id, doc){
if (doc.middleName){ return client.hset(id, "middleName", doc.middleName); }
else { return; }
}
return Q.all([Q.ninvoke(client, 'sadd', 'Doctors', id),
Q.ninvoke(client, 'hmset', id, "lastName", doc.lastName, "firstName", doc.firstName, "email", doc.email, "university", doc.university, "work", doc.work),
Q.ninvoke(client, 'sadd', id + ':fields', fields),
middleName(id, doc)]).then(function(x){
return getInfo(id, req, res, next);;
}, function (err) { res.status(404); });
};
这将传递给函数getInfo(),该函数将向客户端发送响应:
var redisHGetAll = Q.nbind(client.hgetall, client);
var getInfo = function getInfo(id, req, res, next){
return redisHGetAll(id).then(function(x){
return findByMatchingProperties(x);
}, function (err) { res.status(404); }).then(function(){
return client.smembers(id + ':fields', function(err, reply){
data['fields'] = reply;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(data));
});
}, function (err) { res.status(404); })
};
function findByMatchingProperties(x) {
for (var y in x){
checkData(y, x[y]);
}
function checkData(y, z){
for (var d in data){
if (d === y){
data[d] = z;
}
}
}
}
var data = {
lastName: null,
firstName: null,
middleName: null,
email: null,
university: null,
work: null,
fields: null
};