【问题标题】:Why variable shows up as undefined?为什么变量显示为未定义?
【发布时间】:2013-07-22 13:38:05
【问题描述】:

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

            } 
        }
    });
};

【问题讨论】:

  • 欢迎来到 async 的精彩世界!你需要在回调中。
  • The Question 相关?
  • 另外,你有一个错误的x,你的标签很糟糕。
  • 是的,抱歉,它实际上是一段更大代码的 sn-p,x 是偶然出现的,因为我从文件中删除了 cmets,抱歉...我更新了包含其余部分的代码
  • 我不在回调中吗?我在 users.forEach 中的函数内

标签: javascript node.js mongodb mongoose


【解决方案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
                        });
                    }
                );
            } 
        }
    });
};

【讨论】:

  • 我将如何实现异步?如果没有,那么我仍然没有使用您上面写的内容将 userAdd 设为未定义。
  • 是的,我查看了 async,老实说,我无法理解如何在这里使用它。
  • 表示第二个函数的 user._id 未定义
猜你喜欢
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 2023-03-06
  • 2018-02-27
相关资源
最近更新 更多