我得到的错误是:
C:\Users\brend\project\server\node_modules\sequelize\lib\sequelize.js:392
this.importCache[path] = defineCall(this, DataTypes);
^
TypeError: defineCall is not a function
at Sequelize.import (C:\Users\brend\project\server\node_modules\sequelize\lib\sequelize.js:392:32)
at fs.readdirSync.filter.forEach (C:\Users\brend\project\server\src\models\index.js:20:35)
我在学习节点,vue和一堆图书馆的时候,我还没有能够解决这个问题。
index.js如下:
const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
const config = require('../config/config')
const db = {}
const sequelize = new Sequelize(
config.db.database,
config.db.user,
config.db.password,
config.db.options
)
fs
.readdirSync(__dirname)
.filter((file) =>
file !== 'index.js'
)
.forEach((file) => {
const model = sequelize.import(path.join(__dirname), file)
db[model.name] = model
})
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
这是我的User.js,这是我目前唯一的模型:
module.exports = (sequelize, DataTypes) =>
sequelize.define('User', {
email: {
type: DataTypes.STRING,
unique: true
},
password: DataTypes.STRING
})
如有必要,我的文件夹结构:
-src
--app.js
--routes.js
--config
----config.js
--controllers
----AuthenticationController.js
--models
----index.js
----User.js
还有我的app.js sequelize.sync()
中未定义的内容,至少是我的IDE所指示的内容。
答案 0 :(得分:11)
我在github sequelize github上找到了,替换
const model = sequelize.import(path.join(__dirname, file));
使用
var model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
它对我来说适用于sequelize 6.3.0
答案 1 :(得分:11)
sequelize.6中不推荐使用sequelize.import。在那里 直到v5
https://sequelize.org/master/manual/upgrade-to-v6.html#sequelize https://sequelize.org/master/manual/models-definition.html
const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
const config = require('../config/config')
const db = {}
const sequelize = new Sequelize(
config.db.database,
config.db.user,
config.db.password,
config.db.options
)
fs
.readdirSync(__dirname)
.filter((file) =>
file !== 'index.js'
)
.forEach((file) => {
const model = sequelize.import(path.join(__dirname), file) // error occurs here
db[model.name] = model
})
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
sequelize.import
。const fs = require('fs')
const path = require('path')
const Sequelize = require('sequelize')
const config = require('../config/config')
const db = {}
const sequelize = new Sequelize(
config.db.database,
config.db.user,
config.db.password,
config.db.options
)
fs
.readdirSync(__dirname)
.filter((file) =>
file !== 'index.js'
)
.forEach((file) => {
/**
* Choose any one of the below definition for model variable
*/
// for individual model files having `export default (sequelize, DataTypes) => {`
const model = require(path.join(__dirname, file)).default(sequelize, Sequelize.DataTypes);
// for individual model files having `module.exports = (sequelize, DataTypes) => {`
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
})
db.sequelize = sequelize
db.Sequelize = Sequelize
module.exports = db
注意事项:
如果在模型文件中同时使用export default ...
和babel->我们正在使用require(...).default
as require(...)
不会返回函数,而是返回对象为{ default: [Function] }
并因此使用它的属性'default'
答案 2 :(得分:2)
“ sequelize.import”已被弃用。您应该改用“ require”。
请参阅上面的 artfulbeest 解决方案。它对我有用。
更多内容请阅读官方文档(https://sequelize.org/master/manual/models-definition.html)
答案 3 :(得分:1)
检查您正在使用的续集的版本。当我使用sequelize@6.0.0-beta.6
并发现序列对象/客户程序中缺少import
时,我遇到了持续的错误:
npm卸载续集
npm install sequelize
答案 4 :(得分:0)
<强>尝试:强>
<强>模型/ index.js:强>
...
fs
.readdirSync(__dirname)
.filter((file) => {
return (file.indexOf(".") !== 0) && (file !== "index.js");
})
.forEach((file) => {
const model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
...
models / user.js:(我用小写字母)
module.exports = (sequelize, Sequelize) => {
var User = sequelize.define('user', {
email: {
type: Sequelize.STRING,
unique: true
},
password: Sequelize.STRING
})
return User;
}
致电model.user:
const model = require('./models'); // Change path if is necesary
model.user.create({ ...
答案 5 :(得分:0)
这是续集的解决方案”:“ ^ 6.3.4 替换
const model = sequelize.import(path.join(__dirname, file));
使用
const model = require(path.join(__dirname, file))(sequelize, Sequelize);