Sequelize - called with something that is not a subclass of Sequelize.Model

I am working on an application, but I have a problem using Node.js and Sequelize for Postgresql:

throw new Error(this.name + '.' + Utils.lowercaseFirst(Type.toString()) + ' called with something that\ not a subclass of Sequelize.Model'); ^ Error: Expense.class BelongsTo extends Association { constructor(source, target, options) { super(source, target, options); <....LOT OF CODE FROM SEQUELIZE ....> if ((fieldsOrOptions || {}).transaction instanceof Transaction) { options.transaction = fieldsOrOptions.transaction; } options.logging = (fieldsOrOptions || {}).logging; return association.target.create(values, fieldsOrOptions).then(newAssociatedObject => sourceInstance[association.accessors.set](newAssociatedObject, options) ); } } called with something that not a subclass of Sequelize.Model 

I do not understand this error, especially the last line, called something that is not a subclass of Sequelize.Model. Here are the models:

User model

 const models = require('./index'); const Expense = models.User; module.exports = (sequelize, DataTypes) => { var User = sequelize.define('User', { firstname: DataTypes.STRING, lastname: DataTypes.STRING, email: DataTypes.STRING, password: DataTypes.STRING }, { classMethods: { associate: function(models) { User.hasMany(models.Expense); } } }); console.log(models); User.hasMany(Expense, {as: 'Expenses', foreignKey: 'userId'}); return User; }; 

And Cost Model

 const models = require('./index'); const User = models.User; module.exports = (sequelize, DataTypes) => { var Expense = sequelize.define('Expense', { name: DataTypes.STRING, amount: DataTypes.INTEGER, date: DataTypes.INTEGER }, { classMethods: { } }); Expense.belongsTo(User, { foreignKey: 'userId' }); return Expense; }; 

And the controller to create the flow:

 createExpense: function(req, res) { const user = User.findOne({ where: { id: req.params.id } }); Expense.create({ name: req.body.name, amount: req.body.amount, date: req.body.date, User: user },{ include: [ { model: User } ] }).then((created) => { res.status(200).send({ success: true, message: 'Dépense ajoutée !' }); }); } 

Has anyone already seen an error that looks like this? I am looking for a few days without any problems, if someone can help, I will be very grateful

thanks!

+5
source share

Source: https://habr.com/ru/post/1273192/


All Articles