#sequelize #belongsToMany #update

问题描述

我有 2 个模型:用户(id、用户名、密码)和角色(id、 name_role),user_role (id,id_user) 创建了我想要的belongToMany 更新用户角色。我试试这个,但它给了我错误。 我对 sequelize 不是很熟悉,我想我犯了一些错误 在我的说明中添加一些额外的东西......非常感谢

exports.updateRole = (req,res) => {
    try {
        const id = req.body.id;
      //  const roles = req.params.roles;
        User.findByPk(id )
            .then(data => {
                if (data) {
                    Role.findAll({
                        where: {
                            name: {
                                [Op.or]: req.body.role
                            }
                        }
                    }).then(roles => {
                        user.setRoles(roles).then(() => {
                            res.status(200).send(error.OK)
                        })
                            .catch(err => {
                                console.log(err);
                                res.status(500).send(error.SERVER_ERROR)
                            });
                    })
                        .catch(err => {
                            console.log(err);
                            res.status(500).send(error.SERVER_ERROR)
                        });
                              
                } else {
                    var result = error.USER_NOT_PRESENT
                    res.status(404).send(result)
                }
            }).catch(err => {
                console.log(err);
                res.status(500).send(error.SERVER_ERROR)
            });
    } catch (e) {
        console.log(e);
        res.status(500).send(error.SERVER_ERROR)
    }
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)