问题
Model not initialized: Member "getTableName" cannot be called. "User" needs to be added to a Sequelize instance
出现错误的地方:
async getAllPosts(body: any) {
const { pageSize, pageIndex, userId } = body;
let query: Object = {};
query["offset"] = (pageIndex !== undefined && pageIndex !== null) ? ((pageIndex - 1) * (pageSize || 10)) : 0;
query["limit"] = (pageSize !== undefined && pageSize !== null) ? pageSize : 10;
if (userId !== undefined && userId !== null) {
query["where"] = {
user_id : userId
};
}
query["include"] = [User];
let result: { rows: HuntingPostForm[]; count: number };
try {
result = await this.huntingPostModel.findAndCountAll(query)
} catch (error) {
throw new CommonException(StatusCode.SYS_ERROR, error.message);
}
}
解决方法:
https://github.com/sequelize/sequelize-typescript/issues/686 只能参考
https://github.com/sequelize/sequelize-typescript#how-to-use-associations-with-repository-mode 可以解决
就是Repository方式的include中,必须还是Repository类型
query["include"] = [this.huntingRoleModel]; // [this.userModel, this.huntingRoleModel]