2013-08-18 43 views
0

我使用的是async npm的filterSeries,但是当我在对象上接下来调用truthy时,出于某种原因,它只传递用户而不是试图从查询中取出的部分。对象没有被传入回调

如果您发现我的代码出了什么问题,或者有更有效的方法来解决这个问题,因为我也听说,循环访问每个用户并调用查询是一个糟糕的主意,而不是执行$ 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}); 
    }); 
}; 
+0

正如目前所写,很难理解你的问题和你遇到的具体问题。 – WiredPrairie

+0

djbrick了解它 – Lion789

回答

1

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

相关问题