これは、ユーザーの結果をループするコードであり、ユーザーからの検索によって作成されたフレンド リクエスト配列をループしようとしています。ただし、 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