I’m new to NodeJs/Sequelize. I have declared this model :
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Ingredient extends Model {
static associate(models) {
Ingredient.belongsTo(models.Department, { foreignKey: 'departmentId' });
}
};
Ingredient.init({
id: {
type: DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true,
},
name:
{
type: DataTypes.STRING,
allowNull: false,
},
departmentId: {
type: DataTypes.INTEGER,
allowNull: false,
}
}, {
sequelize,
modelName: 'Ingredient',
});
return Ingredient;
};
And in my ingredient.controler, in create() method I try to instanciate an Ingredient with :
const ingredient = Ingredient.build({
name: req.body.name,
departmentId: req.body.departmentId,
})
as said in their doc, but I get an error :
Ingredient.build is not a function
I also tried with a new()
const ingredient = new Ingredient({
name: req.body.name,
departmentId: req.body.departmentId,
});
but I also get an error :
Ingredient is not a constructor
What do I miss?