【问题标题】:Sequelize v5 : FindAll in the then of Create doesn't retrieve created rowSequelize v5:在 Create 的 then 中的 FindAll 不检索创建的行
【发布时间】:2020-03-16 06:33:08
【问题描述】:

大家好, 我对使用 MySQL 数据库的 Sequelize v5 有疑问。
当我在执行创建后尝试对模型执行 findAll 时,我没有刚刚创建的模型。 我应该怎么做才能解决这个问题?

这是有问题的代码,以及它的作用:
我创建了一个演员,我将它链接到一个关联表,然后我做了一个 findAll。我确定演员创建良好(存在于 BDD 中,与关联表相同,控制台中显示“提交”,并且我有在 findAll 之前创建的演员的返回 id)。

return Actor.create({
    firstname: req.body.firstname,
    lastname: req.body.lastname,
    mobile: req.body.mobile,
    email: req.body.email,
    city: req.body.city,
    zipcode: req.body.zipcode
  }).then(function (rowCreated1) {
    return Asso_role_school_actor.create({
      id_actor: rowCreated1.id_actor,
      id_role: 1,
      id_school: req.body.id_school
    }).then(function () {
      return Actor.findAll({
        attributes: ['id_actor', 'firstname', 'lastname', 'email', 'mobile', 'city', 'zipcode'],
        include: [{
          model: Asso_role_school_actor,
          where: {
            id_role: 1,
            id_school: req.params.id_school,
            finishAt: null
          },
          attributes: []
        }]
      }).then((results) => {
        console.log(results);
        return res.status(200).send(results);
      });
    });
  });

欢迎所有线索! :)

【问题讨论】:

    标签: sequelize.js


    【解决方案1】:

    您正在使用 req.body.id_school 保存对象,但在 find 方法的包含中您使用 req.params.id_school。也许这是造成问题的原因?

    【讨论】:

    • 不,不幸的是这不是问题:(在其他请求中也发生在我身上
    猜你喜欢
    • 2020-10-12
    • 1970-01-01
    • 2021-03-21
    • 2016-03-13
    • 2019-12-01
    • 2021-05-14
    • 2020-05-08
    • 2020-04-08
    • 2017-04-24
    相关资源
    最近更新 更多