这是我循环遍历用户结果的代码,然后我试图循环遍历用户搜索所产生的好友请求数组。但是,我不断收到 TypeError: cannot read property '$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