Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript “续集”;包括「;不在多对多关系中工作_Javascript_Node.js_Sequelize.js_Many To Many_Associations - Fatal编程技术网

Javascript “续集”;包括「;不在多对多关系中工作

Javascript “续集”;包括「;不在多对多关系中工作,javascript,node.js,sequelize.js,many-to-many,associations,Javascript,Node.js,Sequelize.js,Many To Many,Associations,我是新的节点和续集,我正在建立一个基本的电子商务。 这些是我的型号: const Product = sequelize.define('product', { id: { type: DataTypes.INTEGER, autoIncrement: true, allowNull: false, primaryKey: true, }, name: { type: DataTypes.STRING, allowNull: false

我是新的节点和续集,我正在建立一个基本的电子商务。 这些是我的型号

const Product = sequelize.define('product', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    allowNull: false,
    primaryKey: true,
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
});

const Sale = sequelize.define('sale', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    allowNull: false,
    primaryKey: true,
  },
  isDelivered: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    allowNull: false,
  },
});
Sale.belongsToMany(Product, { through: SaleItem });
Product.belongsToMany(Sale, { through: SaleItem });
这些关系是:

const Product = sequelize.define('product', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    allowNull: false,
    primaryKey: true,
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
  },
});

const Sale = sequelize.define('sale', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    allowNull: false,
    primaryKey: true,
  },
  isDelivered: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    allowNull: false,
  },
});
Sale.belongsToMany(Product, { through: SaleItem });
Product.belongsToMany(Sale, { through: SaleItem });
当我尝试获取“SaleItems”时,问题就开始了

我得到的答复是:

SequelizeAgerLoadingError:产品未与saleItem关联

这很奇怪,因为我在一对多关系上也做了同样的事情,而且工作得很完美


感谢您的时间:)

您在
产品
销售
之间有关联,但在它们和
销售项目
之间没有关联

您现有的关联只允许执行如下查询:

Sale.findAll({
    include: [Product],
  })
  .then(response => {
   console.log(response);
  })
Product.findAll({
    include: [Sale],
  })
  .then(response => {
   console.log(response);
  })
SaleItem.belongsTo(Product);
SaleItem.belongsTo(Sale);
您需要为
SaleItem
添加如下关联:

Sale.findAll({
    include: [Product],
  })
  .then(response => {
   console.log(response);
  })
Product.findAll({
    include: [Sale],
  })
  .then(response => {
   console.log(response);
  })
SaleItem.belongsTo(Product);
SaleItem.belongsTo(Sale);

为了获得
SaleItem
记录以及相关的模型实例。

我发现了类似的问题,也许可以帮助您。祝你好运