【问题标题】:How to store value to var from another function如何将值从另一个函数存储到 var
【发布时间】:2018-12-05 22:10:51
【问题描述】:

我正在比较我的两个名为:user 和 bloodrequest 的文档,如果它们匹配,则显示具有相同 id 的表 bloodrequest 中的值。我的问题是我试图将当前登录的用户存储到这样的 var 中:var permit = mainUser.branch_id 然后使用 $where 语句使用此:this.chapter_id == permit 但给了我错误。

MongoError: TypeError: mainUser is undefined :

这是我的代码,我唯一的问题是如何将mainUser.branch_id 传递给var permit,我才刚刚开始学习

router.get('/bloodapprovedrequestmanagement', function(req, res) {
    User.find({}, function(err, users) {
        if (err) throw err;
        User.findOne({ username: req.decoded.username }, function(err, mainUser) {
            if (err) throw err;
            if (!mainUser) {
                res.json({ success: false, message: 'No user found' });
            } if (mainUser.branch_id === '111') {

                Bloodrequest.find({$where: function(err) {
                        var permit = mainUser.branch_id//gives me error here
                        return (this.request_status == "approved" && this.chapter_id == permit) }}, function(err, bloodrequests) {
                    if (err) throw err;
                    Bloodrequest.findOne({ patient_name: req.decoded.patient_name }, function(err, mainUser) {
                        if (err) throw err;
                        res.json({ success: true, bloodrequests: bloodrequests });
                    });
                });

            }
        });
    });
});

【问题讨论】:

  • 在一个通用范围内定义。但这是反模式
  • if (!mainUser) {... 块中添加return 语句。
  • 我应该改什么部分?,请帮我解决这个问题
  • return res.json({ success: false, message: 'No user found' });
  • 仍未定义 mainUser :

标签: javascript angularjs node.js mongodb mean-stack


【解决方案1】:

在本地范围之外声明变量。

 `router.get('/bloodapprovedrequestmanagement', function(req, res) {
           var permit;
            User.find({}, function(err, users) {
                if (err) throw err; 
                User.findOne({ username: req.decoded.username }, function(err, mainUser) {
                    if (err) throw err; 
                    if (!mainUser) {
                        res.json({ success: false, message: 'No user found' }); 
                    } 
                    if(mainUser.branch_id === '111') {
                        permit = mainUser.branch_id;
                        Bloodrequest.find({$where: function(err) {
                            return (this.request_status == "approved" && this.chapter_id == permit) }}, function(err, bloodrequests) {
                            if (err) throw err; 
                            Bloodrequest.findOne({ patient_name: req.decoded.patient_name }, function(err, mainUser) {
                                if (err) throw err; 
                                res.json({ success: true, bloodrequests: bloodrequests });                     
                            });
                        });

                    }
                });
            });
        });`

【讨论】:

  • 给我:MongoError: ReferenceError: permit is not defined : @:2:29
  • 我已经更新了我的答案。让我们试试吧。将许可声明为全局范围。
  • still ReferenceError: permit is not defined :( 我不知道怎么回事
【解决方案2】:

将您的callback 转换为async await 更简单。

router.get('/bloodapprovedrequestmanagement', function async(req, res) {
   try {
    var permit;
    let mainUser = await User.findOne({ username: req.decoded.username }); 
    if(mainUser && mainUser.branch_id && mainUser.branch_id === '111') {
     permit = mainUser.branch_id;
     // here you add your condiion for (this.request_status == "approved" && this.chapter_id == permit).
     let bloodRequestData = await  Bloodrequest.findOne({ patient_name: req.decoded.patient_name });
     res.json({ success: true, bloodrequests: bloodRequestData }); 
    } 
   } catch (error) {
    throw error
  }
}

据我了解,您没有在代码中使用 User.find({})Bloodrequest.find({}) 数据。

【讨论】:

  • 给了我 let mainUser = await User.findOne({ username: req.decoded.username }); ^^^^ SyntaxError: Unexpected identifier
  • 确保你添加了async,比如function async(req, res)
猜你喜欢
  • 1970-01-01
  • 2011-04-19
  • 2019-10-13
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
  • 1970-01-01
相关资源
最近更新 更多