尝试定义模型时,NodeJS Sequelize引发错误

时间:2019-07-12 02:28:02

标签: node.js sequelize.js

由于抛出错误,我无法在节点应用程序中设置Sequelize

错误如下:

this.importCache[importPath] = defineCall(this, DataTypes);
TypeError: Class constructor model cannot be invoked without 'new'

我认为这已在模型define globals.js的{​​{1}}行中抛出

我尝试在const Communication = db.define('communication', {前面使用new,但是随后抱怨并说db.define不是构造函数。

有什么想法我做错了吗?


db.define

server.js

require("dotenv").config() require("./models") const asyncHandler = require("express-async-handler"), producer = require("./producer"), express = require("express"), app = express(), bodyParser = require("body-parser"), port = process.env.PORT || 5000, utilities = require("./utilities"), db = require("./config/db") // Express Config app.use(bodyParser.json()) // Receive our incoming payload app.post( "/payload", asyncHandler(async (req, res, next) => { let result = await producer.sendToProducer(req.body) res.json(result); }) ) // Begin Listening db.sync({}).then(() => { app.listen(port, () => { utilities.logError(`Server Listening...`) }) })

./config/db.js

const Sequelize = require('sequelize'); module.exports = new Sequelize(process.env.DATABASE_URL);

./models/index.js

const db = require('../config/db'); const models = { Global: db.import('./global'), }; Object.keys(models).forEach((modelName) => { if ('associate' in models[modelName]) { models[modelName].associate(models); } }); module.exports = models;

./models/global.js

1 个答案:

答案 0 :(得分:0)

您的路径是否对global.js模型中的const db不正确?您要转到“ ../config/db”,但文件是“ ../ config / db.js”。

我对节点如何工作的理解是,您的路径通向'../ config / db / index.js'。