如何在mongodb中循环遍历数组?

时间:2013-07-21 06:30:20

标签: node.js mongodb mongoose

这是我循环查看用户结果的代码,然后我试图遍历用户搜索产生的好友请求数组。但是,我一直得到一个TypeError:无法读取属性'$ elemMatch of undefined。我使用的是mongoose和Node.Js,这个searchPost位于路径文件夹中。

exports.searchPost = function(req, res, err) {
    User.find({$or:[
            {firstName: req.body.firstName},
            {lastName: req.body.lastName},
            {email: req.body.email},
            {phone: req.body.phone}]
    }, function(err, users, userAdd) {
        if(err) {

            return res.render('searchError', {title: 'Weblio'}); 
        } else {

            if(req.body.firstName=== '' && req.body.lastName==='' && req.body.email==='' && req.body.phone=== '') {
                return res.render('searchError', {title: 'Weblio'});        
            } else {

                    for(x in users) {

                        User.findById(req.signedCookies.userid, 
                            {friendRequest: x.id}
                                if(x.id === true ) {
                                    console.log('addman1'); 
                                    return userAdd = false;
                                } else {
                                    console.log('addman2');
                                    return userAdd = true;
                                }

                            });

                        }       


                return res.render('searchResults', {title: 'Weblio',        
                    usersFound: users, 
                    userAdded: userAdd
                });
            } 
        }
    });

};

错误:

TypeError: Cannot read property '$elemMatch' of undefined
    at Query._castFields (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoos
e\lib\query.js:2203:22)
    at Query.findOne (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoose\li
b\query.js:1775:25)
    at Function.findOne (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoose
\lib\model.js:944:16)
    at Function.findById (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoos
e\lib\model.js:882:15)
    at Promise.<anonymous> (C:\Users\Lior\Desktop\nodep\test5\routes\user.js:193
:12)
    at Promise.<anonymous> (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongo
ose\node_modules\mpromise\lib\promise.js:162:8)
    at Promise.EventEmitter.emit (events.js:95:17)
    at Promise.emit (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoose\nod
e_modules\mpromise\lib\promise.js:79:38)
    at Promise.fulfill (C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoose\
node_modules\mpromise\lib\promise.js:92:20)
    at C:\Users\Lior\Desktop\nodep\test5\node_modules\mongoose\lib\query.js:1725
:26
[ERROR] 17:23:30 TypeError

2 个答案:

答案 0 :(得分:4)

我想你可能会尝试这样做

var totalDocsToProcess = users.length;

for(var i = 0; i < users.length; i++) {
  User.findById(req.signedCookies.userid, function(err, x) {
    totalDocsToProcess = totalDocsToProcess - 1;

    if(x.id === true ) {
      console.log('addman1'); 
      return userAdd = false;
    } else {
      console.log('addman2');
      return userAdd = true;      
    }

    if(totalDocsToProcess == 0) {
      return res.render('searchResults', {title: 'Weblio',        
        usersFound: users, 
        userAdded: userAdd
      });      
    }
  })
}

答案 1 :(得分:2)

for(x in users) x是数组的关键而不是值,您应该尝试:

for(var i = 0; i < users.length; ++i) { 
    var x = users[i]; etc etc