gpt4 book ai didi

node.js - 无法使用 TypeScript : Error: No Sequelize instance passed 导入 Sequelize 模型

转载 作者:行者123 更新时间:2023-12-03 22:25:50 25 4
gpt4 key购买 nike

我的 user.model.ts 是:

@Table
export class User extends Model<User> {
@Column({ allowNull: false })
firstName: string;

@Column({ allowNull: false })
lastName: string;
...

当我尝试导入它时:
    const sequelize = new Sequelize(DB_CREDENTIALS_PARSED.database, DB_CREDENTIALS_PARSED.username, DB_CREDENTIALS_PARSED.password, { dialect: 'postgres' })
const modelFile = require(path.join(modelPath, 'user.model')).User.init({ sequelize })

console.log(modelFile)

我收到一个错误: Error: No Sequelize instance passed
我究竟做错了什么?

最佳答案

sequelize 实例必须在 init 的第二个参数中传递
方法

const User = require(path.join(modelPath, 'user.model'));
const sequelize = new Sequelize(
DB_CREDENTIALS_PARSED.database,
DB_CREDENTIALS_PARSED.username,
DB_CREDENTIALS_PARSED.password, {
dialect: 'postgres'
}
)
User.init(modelAttributesObject, { sequelize })

但是当您使用 sequelize TypeScript 时,您可以使用实例初始化您的模型
const User = require(path.join(modelPath, 'user.model'));
const sequelize = sequelize = new Sequelize({
dialect: 'postgres',
database: DB_CREDENTIALS_PARSED.database,
username: DB_CREDENTIALS_PARSED.username,
password: DB_CREDENTIALS_PARSED.database,
models: [User]
});

关于node.js - 无法使用 TypeScript : Error: No Sequelize instance passed 导入 Sequelize 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61599849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com