我在一个有效的朋友请求数组中查询一个用户,但是我使用 userAdd 的检查一直显示为未定义。任何人都知道为什么它显示为未定义?
这是代码:
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) {
if(err) {
return res.render('searchError', {title: 'Weblio'});
} else {
if(req.body.firstName=== '' && req.body.lastName==='' && req.body.email==='' && req.body.phone=== '') {
//maybe a diff page saying that is not a valid search page
return res.render('searchError', {title: 'Weblio'});
} else {
var userAdd;
console.log('addman');
users.forEach(function(user) {
User.findById(req.signedCookies.userid,
{friendRequest: user._id}, function() {
if(user._id === true ) {
console.log('addman1');
return userAdd = true;
} else {
console.log('addman2');
return userAdd = false;
console.log(user._id);
}
})
});
console.log(userAdd);
return res.render('searchResults', {title: 'Weblio',
usersFound: users,
userAdded: userAdd
});
}
}
});
};