model.has许多调用的东西不是Sequelize.Model的子类



每当我试图从无服务器框架lambda 调用任何调用时,我都会收到以下错误

[offline] _____ HANDLER RESOLVED _____
offline: Failure: product.hasMany called with something that's not a subclass of Sequelize.Model
Error: product.hasMany called with something that's not a subclass of Sequelize.Model
at Function.hasMany (C:UsersKiranDocumentsProjectsRentals-Backendnode_modulessequelizelibassociationsmixin.js:18:13)
at Function.Product.associate (C:UsersKiranDocumentsProjectsRentals-Backendentityproduct.js:21:17)

IMPORTANT

以下代码是上述错误的答案。你可能会错过任何一步。所以你可以参考和修正。感谢阿纳托利帮助我解决了这个问题。

产品型号

const { STRING, BOOLEAN, INTEGER } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
const Product = sequelize.define("product", {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
name: { type: STRING },
description: { type: STRING, allowNull: true },
purchase_price: { type: STRING },
tax: { type: STRING },
sale_price: { type: STRING },
categoryId: { type: STRING },
status: { type: BOOLEAN, defaultValue: 0 },
created_on: { type: INTEGER, allowNull: true },
updated_on: { type: INTEGER, allowNull: true },
}, {
timestamps: false,
freezeTableName: true,
})
Product.associate = function (models) {
Product.hasMany(models.product_image, { as: "images" });
Product.belongsTo(models.product_category, { as: "category", foreignKey: 'categoryId' });
};
return Product;
}

图像模型

const { STRING, BOOLEAN, INTEGER } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
const ProductImage = sequelize.define("product_image", {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
productId: { type: INTEGER },
fileName: { type: STRING },
url: { type: STRING },
position: { type: INTEGER },
isDefault: { type: BOOLEAN, defaultValue: 0 },
shopId: { type: STRING },
status: { type: BOOLEAN, defaultValue: 0 },
created_on: { type: INTEGER, allowNull: true },
updated_on: { type: INTEGER, allowNull: true },
}, {
timestamps: false,
freezeTableName: true,
})
return ProductImage;
}

类别模型

const { STRING, BOOLEAN, INTEGER } = require("sequelize");

module.exports = (sequelize, DataTypes) => {
const ProductCategory = sequelize.define("product_category", {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
name: { type: STRING },
description: { type: STRING, allowNull: true },
status: { type: BOOLEAN, defaultValue: 0 },
created_on: { type: INTEGER, allowNull: true },
updated_on: { type: INTEGER, allowNull: true },
}, {
timestamps: false,
freezeTableName: true,
});
return ProductCategory;
}

这是我们初始化sequelize 的配置文件

配置文件

const Sequelize = require('sequelize')
const fs = require('fs')
const path = require('path')
const db = {}
const models = path.join(__dirname, '..', 'entity')
var basename = path.basename(module.filename)

const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASSWORD,
{
dialect: 'mysql',
host: process.env.DB_HOST,
port: process.env.DB_PORT,
logging: false
}
)

fs
.readdirSync(models)
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')
})
.forEach(function (file) {
var model = require(path.join(models, file))(
sequelize,
Sequelize.DataTypes
);
db[model.name] = model;

})

Object.keys(db).forEach(function (modelName) {
if (db[modelName].associate) {
db[modelName].associate(db)
}
})

db.Sequelize = Sequelize
db.sequelize = sequelize

module.exports = db

我们在这里调用产品详细信息。

调用功能

const db = require('../config/sequelize-config');
exports.getProductById = (query, username, shopId) => {
return new Promise((resolve, reject) => {
db.product.findOne({
where: {
id: query.id
},
attributes: ['id', 'name', 'description', ['purchase_price', 'purchasePrice'], 'tax', ['sale_price', 'salePrice']],
include: [{
model: db.product_image,
as: 'images',
where: {
status: 1
},
required: false,
attributes: ['id', 'fileName', 'position', 'url']
},
{
model: db.product_category,
as: 'category',
required: false,
attributes: ['id', 'name']
}]
}).then(product => {
if (product) {
resolve({ [KEY_STATUS]: 1, [KEY_MESSAGE]: "Product details fetched successfully", [KEY_DATA]: product });
} else {
reject({ [KEY_STATUS]: 0, [KEY_MESSAGE]: "Product details fetch failed" });
}
}).catch(error => {
reject({ [KEY_STATUS]: 0, [KEY_MESSAGE]: "Product details fetch failed", [KEY_ERROR]: error.message });
});
})
}

为了避免交叉引用错误和类似错误,我建议将模型定义转换为函数,并在同一个模块中注册模型和关联,请参阅以下答案和问题

最新更新