0

我在一个有效的朋友请求数组中查询一个用户,但是我使用 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
                });

            } 
        }
    });
};
4

1 回答 1

1

它是异步的,因此您需要使用回调方式来链接操作。您可以尝试一些节点模块,如asyncstep以更好的语法实现它。

顺便说一句,我认为您不需要遍历users.forEach(function(user).

如果你使用 Step,你可以做

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');

                Step(
                    function(){
                        User.findById(req.signedCookies.userid, {friendRequest: user._id}, this);
                    },

                    function(err, user){
                        if(user !== null && user._id === true ) {
                            console.log('addman1'); 
                            userAdd = true;
                        } else {
                            console.log('addman2');
                            userAdd = false;
                            console.log(user._id);  
                        }

                        return res.render('searchResults', {title: 'Weblio',        
                            usersFound: users, 
                            userAdded: userAdd
                        });
                    }
                );
            } 
        }
    });
};
于 2013-07-22T05:15:36.090 回答