Express Node.JS - Receiving Redis callback, executing promises

流过昼夜 提交于 2019-12-13 00:52:51

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/34902092/express-node-js-receiving-redis-callback-executing-promises

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!