【发布时间】: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