model.hasMany 调用的东西不是 Sequelize.Model 的子类
Posted
技术标签:
【中文标题】model.hasMany 调用的东西不是 Sequelize.Model 的子类【英文标题】:model.hasMany called with something that's not a subclass of Sequelize.Model 【发布时间】:2022-01-04 23:16:39 【问题描述】:每当我尝试从无服务器框架 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:\Users\Kiran\Documents\Projects\Rentals-Backend\node_modules\sequelize\lib\associations\mixin.js:18:13)
at Function.Product.associate (C:\Users\Kiran\Documents\Projects\Rentals-Backend\entity\product.js:21:17)
IMPORTANT
以下代码是上述错误的答案。您可能错过了任何步骤。所以你可以参考和修复。感谢Anatoly帮助我解决问题。
产品型号:
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 );
);
)
【问题讨论】:
【参考方案1】:为避免交叉引用错误和类似错误,我建议将模型定义转换为函数并在同一个模块中注册模型和关联,请参阅this answer 和问题
【讨论】:
尝试了几个小时后,我收到了TypeError: Cannot read property 'define' of undefined
您能否用修改后的模型文件和注册模块更新您的问题?
我在底部添加了Config file
和calling funtion
。但是错误来自category model
文件let categoryModel = db.sequelize.define("product_category",
这一行。
你没有像这里那样把你的模型定义变成函数:***.com/a/61710568/1376618
另外,我再次参考了文档,不会提及问题或注意事项。以上是关于model.hasMany 调用的东西不是 Sequelize.Model 的子类的主要内容,如果未能解决你的问题,请参考以下文章
Akka 将 websocket 流的东西流式传输到 Sink.seq 以异常 SubscriptionWithCancelException$StageWasCompleted 结尾
toDF 不是 Seq 的成员,toDS 不是 Seq 的成员
Seq2Seq/NLP/Translation:生成目标句子后,最后的解码器隐藏状态是不是带有任何残余意义?
Sensitivity, specificity, and reproducibility of RNA-Seq differential expression calls RNA-Seq差异表达调用