javascript - 为什么 Sequelize 只使用 findAll() 返回一个对象?

标签 javascript node.js express sequelize.js

我的目标是能够通过品牌名称和型号名称找到所有产品。但是,Sequelize 仅返回许多其他类似记录中的一个记录。如果它确实返回了多个记录,则与找到的第一条记录具有相同属性的其他记录将是 null 。例如,数组中的第一条记录将具有属性 name: iPhone ,具有完全相同属性的第二条记录将显示为 name: null ,而它应该是 name: iPhone

在我的数据库中,我有以下表格:
ProductsBrandsModelsSuppliersProducts 表包含 brand_idmodel_id 等外键。 BrandsModelsSuppliers 具有属性: id

我已将关系设置如下:

Products.hasOne(Brands, { foreignKey: 'id' });
Products.hasOne(Models, { foreignKey: 'id' });
Products.hasOne(Suppliers, { foreignKey: 'id' });

Brands.belongsTo(Products);
Models.belongsTo(Products);
Suppliers.belongsTo(Products);

在我的搜索功能中,我尝试按品牌和型号名称查找与我的查询匹配的所有产品。
const getSearch = (req, res) => {
  const { query: { query } } = req;

  Products.findAll({
    where: Sequelize.where(Sequelize.fn('concat', Sequelize.col('Brand.name'), ' ', Sequelize.col('Model.name')), {
      [Op.substring]: query
    }),
    include: [
      { model: Brands, attributes: ['name'] },
      { model: Models, attributes: ['name'] },
      { model: Suppliers, attributes: ['name'] },
    ],
    attributes: ['id', 'price']
  })
    .then((data) => {
      console.log(data);
      res.send(data);
    })
    .catch((err) => (console.log(err)));
};

在我的数据库中,我有两个具有完全相同数据但不同 id 的产品行。调用 getSearch 时,我希望在数组中看到两个对象,因为它们具有相同的品牌名称和型号名称。相反,我看到了一个。

这是我的模型的样子:

产品
class Products extends Model {
  static init(sequelize, DataTypes) {
    return super.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true
        },
        url_id: {
          type: DataTypes.INTEGER,
          allowNull: true
        },
        brand_id: {
          type: DataTypes.INTEGER,
          allowNull: true
        },
        model_id: {
          type: DataTypes.INTEGER,
          allowNull: true
        },
        supplier_id: {
          type: DataTypes.INTEGER,
          allowNull: true
        },
        image: {
          type: DataTypes.STRING,
          allowNull: true
        },
        description: {
          type: DataTypes.STRING,
          allowNull: true
        },
        price: {
          type: DataTypes.DOUBLE,
          allowNull: true
        }
      },
      {
        modelName: 'Products',
        timestamps: false,
        sequelize
      }
    );
  }
}

楷模
class Models extends Model {
  static init(sequelize, DataTypes) {
    return super.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true
        },
        name: {
          type: DataTypes.STRING,
          allowNull: true
        },
        colour_id: {
          type: DataTypes.INTEGER,
          allowNull: true
        },
        storage_capacity_id: {
          type: DataTypes.INTEGER,
          allowNull: true
        }
      },
      {
        modelName: 'Models',
        timestamps: false,
        sequelize
      }
    );
  }
}

品牌
class Brands extends Model {
  static init(sequelize, DataTypes) {
    return super.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true
        },
        name: {
          type: DataTypes.STRING,
          allowNull: true
        }
      },
      {
        modelName: 'Brands',
        timestamps: false,
        sequelize
      }
    );
  }
}

供应商
class Suppliers extends Model {
  static init(sequelize, DataTypes) {
    return super.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true
        },
        name: {
          type: DataTypes.STRING,
          allowNull: true
        }
      },
      {
        modelName: 'Suppliers',
        timestamps: false,
        sequelize
      }
    );
  }
}

我在这里做错了什么?

最佳答案

您有关联错误。只需将 hasOne 更改为 hasMany 即可。

关于javascript - 为什么 Sequelize 只使用 findAll() 返回一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57749107/

相关文章:

node.js - 如何将 socket.io 和 Express.js 与 IISNode 结合使用

javascript - 查看控制台是否可用

javascript - 将嵌套数组值映射到 Map,具有按数组索引设置 Map 值的功能,这将通过引用反射(reflect)在嵌套数组中

javascript - 是的模式验证 : Exclude a certain pattern in the method '.matches()'

node.js - 如何将串口传入的缓冲区数据转换为nodejs

node.js - 无法在 GAE 上部署 node.js 应用程序

javascript - 如何测试更改组件状态的函数?

javascript - Mongoose - 分组、计数并在集合中找不到任何内容时返回 0

javascript - 每次循环存储随机顺序的数组

javascript - express.js ejs 不渲染我的 html