【问题标题】:Error with JS roles TypeError: Cannot read property 'push' of undefinedJS 角色类型错误出错:无法读取未定义的属性“推送”
【发布时间】:2018-04-29 13:05:26
【问题描述】:

我在网站的管理面板中设置角色时遇到了一点问题。我设置的角色是 mmUser、mmMerchant、mmAdmin

当用户注册时,它们会自动设置为 mmUser,在管理面板中,我尝试将它们设置为 mmMerchant,现在我可以通过 MongoDB 设置手动完成。奇怪的是,我可以通过面板将它们从商家降级为 mmUser,但不能将它们作为商家。

有什么想法吗?

Controller.get('/make-merchant/:id/', function(req, res, next){
    let id = req.params.id;

    Collections
    .Schemas
    .Users
    .find()
    .then(results => {
        results.roles.push('mmMerchant');
        results.save()
        .then(user=>{
            res.redirect('/management/users/');
        })
        .catch(next)
    })
    .catch(next)
});

错误日志说明了这一点

TypeError: Cannot read property 'push' of undefined
Cannot read property 'push' of undefined
TypeError: Cannot read property 'push' of undefined
    at Collections.Schemas.Users.find.then.results (D:\pre200\pre\routes\management\users.js:69:22)
    at process._tickCallback (internal/process/next_tick.js:103:7)

最新代码

Controller.get('/make-merchant/:id/', function(req, res, next){
    let id = req.params.id;

    Collections
    .Schemas
    .Users
    .find({_id: id })
    .then(results => {
       results[0].roles.push('mmMerchant');
        results.save()
        .then(user=>{
            res.redirect('/management/users/');
        })
        .catch(next)
    })
    .catch(next)
});

【问题讨论】:

  • results.roles.push('mmMerchant'); 失败。 results.roles 未定义。 console.log(results) 能给你什么?
  • @Kristianmitk 控制台说明了这个Caught exception: Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader
  • 您在哪里添加了 console.log(..) ?放在第一个.then()之后
  • @Kristianmitk 好吧,我的错误是我把它放在第二次捕获之后的最后,现在它在 Web 应用程序中的每个功能中都出现了 [model]。似乎它没有添加到角色的数组中。角色:数组[2] 0:“mmUser”1:“”

标签: javascript arrays json node.js mongodb


【解决方案1】:

Model.find 将返回一个文档数组,所以它是results[0].roles

您可以使用Model.findOne 或这里的Model.findById 这只会返回一个文档(或null)。

如果可能,您还可以直接使用 Model.findByIdAndUpdate 之类的内容更新文档

例如,您可以使用$push 运算符:

Model.findByIdAndUpdate(id, { 
  $push: { roles: 'mmMerchant' } 
})

另一种选择是$addToSet,这样可以防止重复。

【讨论】:

  • 嘿,谢谢你的遮阳篷,我应该在哪里改变以适应 Model.FindByIdAndUpdate?
  • 加了个例子,mongodb中还有其他操作数组的操作。
  • 现在看到由于某种原因我收到错误TypeError: results.save is not a function 我一定在某个地方漏掉了一些东西。有关最新代码,请参阅已编辑的问题。
  • 我猜这是猫鼬的问题,老实说,我不经常使用猫鼬。有时间我去看看。
  • 嘿伙计,你有机会看看吗?
【解决方案2】:

更新,

我知道已经修复了这个错误,对于其他遭受同样错误的人。

这是我的代码。

Controller.get('/make-merchant/:id/', function(req, res, next){
    let id = req.params.id;

    Collections
    .Schemas
    .Users
    .findOne({_id: id})
    .then(results => {
             let merchantIndex = results.roles.indexOf('mmUser');

        if(merchantIndex > -1){
            results.roles.push('mmMerchant');
        }

        results.save()
        .then(user=>{
            res.redirect('/management/users/');

        })
        .catch(next)
    })
    .catch(next)
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2018-01-21
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    相关资源
    最近更新 更多