首页 > 解决方案 > Sequelize v5:在 Create 的 then 中的 FindAll 不检索创建的行

问题描述

大家好,我在使用 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

解决方案


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


推荐阅读