【问题标题】:Object not being passed into callback对象未传递给回调
【发布时间】: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


【解决方案1】:

异步过滤器函数接受一个项目数组,并根据真或假回调从该数组中过滤掉项目。因此,您将取回传递给过滤器的原始数组的子集。在这种情况下是用户,我相信你试图建立一个朋友对象并返回它,这是行不通的。您应该做的只是查询数据库中所有具有适当状态的朋友,而不是使用过滤器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-22
    • 2015-12-16
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多