我想通了。我使用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};


