【发布时间】:2013-08-20 12:01:04
【问题描述】:
我正在使用异步 npm 的 filterSeries,但是当我在对象上调用真值 next 时,由于某种原因,它只传递了用户,而不是试图从查询中取出的部分...
如果您注意到我的代码有什么问题或者有更有效的方法来解决这个问题,因为我也听说循环遍历每个用户并调用查询是一个坏主意,而不是执行 $in 或其他操作,但是不知道怎么做。
主要是我想合并两个文档并将其作为数据反馈...
代码如下:
exports.searchContactPost = function(req, res) {
if(req.body.searchContacts === '') { res.send('Oops you searching for nothing, well here is nothing!'); };
async.waterfall([
function(callback) {
User.find({$or:[
{firstName: req.body.searchContacts.toLowerCase()},
{lastName: req.body.searchContacts.toLowerCase()},
{email: req.body.searchContacts.toLowerCase()}]
}, function(err, users) {
if(err || users.length === 0) { res.send(err);}
callback(null, users)
});
},
function(users, callback) {
async.filterSeries(users, function(user, next) {
console.log(user);
Friend.findOne({userId: req.signedCookies.userid, friend_id: user}, function(err, friend) {
if(err) {
console.log("houston we got a problem.")
}
var object = {'fav': friend.favorites, 'notes': friend.notes, 'labels': friend.labels, 'user': user, 'status':friend.friend_status};
console.log(friend);
next(object.status === 3);
})
}, function(friendResults){
console.log(friendResults);
callback(null, friendResults);
});
}
],
function(err, results) {
res.render('contactListResults', {title: 'Weblio', friendsFound: results});
});
};
【问题讨论】:
-
正如目前所写,很难理解您的问题和您遇到的具体问题。
-
djbrick 明白了
标签: node.js mongodb asynchronous mongoose