I have a Node/Express routing function which executes a Redis call in another module. I want to perform a complex Redis function in one node module and send a simple callback saying it is a success to the routing module. The Redis call executes, but I cannot perform any synchronous functions which retrieves even a simple true value from the Redis call. Here is my Redis function:
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;
}
});
};
Everything is working on that end. Now I want the callback to be sent to the Express router to send a response to the client side. I want it to be in the form of a synchronous function, and I've tried many using Q and Async, but nothing is working. So either A. I don't have a full grasp on promise functions, or B. I don't have a full grasp of returning values to another module. Any help would be appreciated.
For reference, here are many failed attempts on the express router end:
routes.js
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);
// });
});