没有Sequelize实例通过
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了没有Sequelize实例通过相关的知识,希望对你有一定的参考价值。
我试图从我的dbController外包我的dbInit
函数模型,因为我有几个模型使dbController变大。
所以我从我的initDb
调用db_controller.js
看起来像这样(我使用那个文件http://docs.sequelizejs.com/manual/getting-started.html)
const userModel = require('../model/user')
const subjectModel = require('../model/subject')
const Sequelize = require('sequelize')
const seq = new Sequelize({
dialect: 'sqlite',
storage: './user.db'
})
async function initDb () {
await userModel.user.initUser()
await subjectModel.subject.initSubject()
userModel.user.userClass.hasMany(subjectModel.subject.subjectClass)
}
user.js
中的用户看起来像这样:
const Sequelize = require('sequelize')
const seq = new Sequelize({
dialect: 'sqlite',
storage: './user.db'
})
class User extends Sequelize.Model {
}
exports.user = {
initUser: initUser,
userClass: User
}
async function initUser () {
return new Promise(resolve => {
User.init(
// attributes
{
firstName: {
type: Sequelize.STRING,
allowNull: false
},
lastName: {
type: Sequelize.STRING,
allowNull: false
},
email: {
type: Sequelize.STRING,
allowNull: false
}
},
// options
{
seq,
modelName: 'user'
}
)
resolve()
})
}
和subject.js
几乎相同
const Sequelize = require('sequelize')
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: './user.db'
})
class Subject extends Sequelize.Model {
}
exports.subject = {
initSubject: initSubject,
subjectClass: Subject
}
async function initSubject () {
return new Promise(resolve => {
Subject.init(
// attributes
{
name: {
type: Sequelize.STRING,
allowNull: false
}
},
// options
{
seq: sequelize,
modelName: 'subject'
}
)
resolve()
})
}
所以当我尝试通过node db_controller.js
执行此操作时
我收到此错误(缩短)
(node:12444) UnhandledPromiseRejectionWarning: Error: No Sequelize instance passed
at Function.init (D:Gitppb
ode_modulessequelizelibmodel.js:915:13)
at resolve (D:Gitppbsrcmodeluser.js:26:10)
at new Promise (<anonymous>)
at Object.initUser (D:Gitppbsrcmodeluser.js:25:10)
at initDb (D:Gitppbsrccontrollerdb_controller.js:18:24)
at Object.<anonymous> (D:Gitppbsrccontrollerdb_controller.js:45:1)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
非常感谢你提前的建议!
答案
我认为你在选项中传递seq
而不是sequelize
是很奇怪的。
检查这里的文档http://docs.sequelizejs.com/我在示例中看到他们通过属性sequelize
而不是seq
。
所以我建议改变:
{
seq: sequelize,
modelName: 'subject'
}
至
{
sequelize: sequelize,
modelName: 'subject'
}
以上是关于没有Sequelize实例通过的主要内容,如果未能解决你的问题,请参考以下文章