Express Node.JS - Receiving Redis callback, executing promises
Asked Answered
D

1

1

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);
    // });
});
Deering answered 20/1, 2016 at 13:59 Comment(0)
D
0

I figured this out. I used the Q library to perform all the functions instead of client.multi().exec(). This allowed a clean execution of all the redis post commands and then allowed for me to retrieve the information.

In the routes.js file, I only had a brief bit of code. Everything is executed in the doctorDB.js file.

routes.js

app.post('/addDoctorInfo', ensureLoggedIn('/login'), function(req, res, next){
        return doctorDB.addDoctor(req.body.id, req.body.doc, req, res, next);
});

doctorDB.js

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); });
};

This gets passed on to the function getInfo() which sends a response to the client side:

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
};
Deering answered 23/1, 2016 at 1:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.