Im setting up my nodejs and graphql api. Using Sequelize as ORM i created my migrations and seeders starting from sequelize-init. Now when i run my app (npm start) an error comes up. I did some searching and the closest answer I found is on this link below but it's not doing anything for me at all.
TypeError: Class constructor model cannot be invoked without 'new'
'use strict'; const fs = require('fs'); const path = require('path'); const Sequelize = require('sequelize'); const basename = path.basename(__filename); const env = process.env.NODE_ENV || 'development'; const config = require(__dirname + '/../config/config.json')[env]; const db = {}; let sequelize; if (config.use_env_variable) { sequelize = new Sequelize(process.env[config.use_env_variable], config); } else { sequelize = new Sequelize(config.database, config.username, config.password, config); } fs .readdirSync(__dirname) .filter(file => { return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); }) .forEach(file => { // const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); const modelDir = require(path.join(__dirname, file)); const model = new modelDir(sequelize, Sequelize.DataTypes) db[model.name] = model; }); Object.keys(db).forEach(modelName => { if (db[modelName].associate) { db[modelName].associate(db); } }); db.sequelize = sequelize; db.Sequelize = Sequelize; module.exports = db;
Error:
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes); ^ TypeError: Class constructor model cannot be invoked without 'new'
I've tried to use 'new' this way
const modelDir = require(path.join(__dirname, file)); const model = new modelDir(sequelize, Sequelize.DataTypes)
but it just results to this error
TypeError: modelDir is not a constructor
https://stackoverflow.com/questions/66961238/class-constructor-model-cannot-be-invoked-without-new April 06, 2021 at 08:14AM
没有评论:
发表评论