【问题标题】:Missing where attribute in the options parameter - sequelizeoptions 参数中缺少 where 属性 - sequelize
【发布时间】:2018-02-14 05:25:17
【问题描述】:

我正在使用 sequelize 版本 4.8.0。根据文档http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-update,这看起来应该是正确的,但是,我收到了这个错误:Missing where attribute in the options parameter

const role = req.body;

sequelize.transaction((t)=> {
    return sequelize.models.role.update(role, {where: req.params, transaction:t}).then(() => {
        res.json({success: true});          
    })
  })
  .catch(errorCatch(res));
}

记录req.params 表明它是这样的:{ id: 'f212d399-f139-4d76-b892-7c2a83281f9b' } 所以这不是问题所在。 谁能告诉我我做错了什么。我认为这可能与交易有关。

【问题讨论】:

    标签: node.js transactions sequelize.js


    【解决方案1】:

    我将此端点作为 GraphQL 突变,它使用基本相同的代码。我还将我的续集更改为 3.30.2,但这与它在突变中的工作没有任何关系,因为我也在端点上尝试过。

    const UpdateRole = {
     type: ResponsePayload,
     args: baseUpdate,
     resolve: (root, role, context) => {
      return joiValidate({
        permission: "WriteRole",
        obj: role,
        joiModel: roleJoiModel,
        user: context.user
      }).then(() => {
      return sequelize.transaction(t => {
        return sequelize.models.role
          .update(role, { where: { id: role.id } }, { transaction: t })
          .then(() => {
            return sequelize.models.role.findOne({ where: { id: role.id } });
          })
          .then(r => {
            return Promise.all([
              r.setPermissions(role.permissions || [], { transaction: t }),
              r.setUsers(role.users || [], { transaction: t })
            ]);
          })
          .then(r => {
            if (role.isDefault) {
              return sequelize.models.role
                .update(
                  { isDefault: false },
                  {
                    where: {
                      id: {
                        $ne: role.id
                      }
                    }
                  }
                )
                .then(() => {
                  return Promise.resolve({ success: true });
                });
            } else {
              return Promise.resolve({ success: true });
            }
          });
         });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 2020-03-25
      相关资源
      最近更新 更多