类型错误:sequelize.import 不是函数 [英] TypeError: sequelize.import is not a function
问题描述
我遇到了一个错误,我找不到解决方法:
I am running into an error and I cannot find a solution to it:
C:\Users\student\Desktop\project\src\models\index.js:17
User: sequelize["import"]('./user'),
^
TypeError: sequelize.import is not a function
at Object.<anonymous> (C:\Users\student\Desktop\project\src\models\/index.js:13:9)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Module._compile (C:\Users\student\Desktop\project\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Object.newLoader [as .js] (C:\Users\student\Desktop\project\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (C:\Users\student\Desktop\project\src\/index.js:9:1)
[nodemon] app crashed - waiting for file changes before starting...
代码如下:
import Sequelize from 'sequelize';
const sequelize = new Sequelize(
process.env.DATABASE,
process.env.DATABASE_USER,
process.env.DATABASE_PASSWORD,
{
dialect: 'postgres',
},
);
const models = {
User: sequelize.import('./user'),
Message: sequelize.import('./message'),
};
Object.keys(models).forEach(key => {
if ('associate' in models[key]) {
models[key].associate(models);
}
});
export { sequelize };
export default models;
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/node": "^7.12.6",
"@babel/preset-env": "^7.12.7",
"nodemon": "^2.0.6"
},
"dependencies": {
"apollo-server": "^2.19.0",
"apollo-server-express": "^2.19.0",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"graphql": "^15.4.0",
"pg": "^8.5.1",
"sequelize": "^6.3.5",
"uuid": "^8.3.1"
我不确定解决问题的正确方向.如果您遇到类似的问题,我们将不胜感激.我添加了开发依赖项以防万一.
I am not sure the right direction to look to solve the issue. Any help is appreciated if you've run into something similar. I've added the dev dependencies in case that helps.
推荐答案
我相信当他们移至 6 时发生了一些变化......查看此链接......或者你可以按照这里的建议返回到 5.22.3线程
i believe something has changed when they moved to 6.... check out this link.. or you could go back to 5.22.3 as suggested in this thread
我目前使用的是 5.22.3,我的语法与您完全相同,并且运行良好,但即将升级到 6,并且还需要解决这些问题.
i am currently on 5.22.3 and i have the exact syntax as you and it works fine, but about to upgrade to six and will need to fix these problems as well..
这篇关于类型错误:sequelize.import 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!