2016-01-20 122 views
1

我有一个Node/Express路由功能,在另一个模块中执行Redis呼叫。我想在一个节点模块中执行一个复杂的Redis函数,并发送一个简单的回调,告诉它路由模块是成功的。 Redis调用会执行,但我无法执行任何同步函数,它们甚至从Redis调用中检索一个简单的真实值。这里是我的Redis的功能:快速Node.JS - 接收Redis回调,执行承诺

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

一切努力实现这一目标。现在我想要将回调发送到Express路由器以向客户端发送响应。我希望它是一个同步函数的形式,我已经尝试了许多使用Q和Async,但没有任何工作。所以要么A.我没有完全掌握承诺函数,或者B.我没有完全掌握将值返回给另一个模块。任何帮助,将不胜感激。

以供参考,在这里是在快递路由器到底有多少失败尝试:我想通了这一点

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

回答

0

。我使用Q库来执行所有的功能,而不是client.multi()。exec()。这允许清理执行所有redis post命令,然后允许我检索信息。

在routes.js文件中,我只有一小段代码。一切都在doctorDB.js文件中执行。

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

这得到到函数的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 
};