Reputation: 385
This is my model which is create from sequelize cli: (it describes an user in User.js)
'use strict';
module.exports = (sequelize, DataTypes) => {
const User = sequelize.define('User', {
username: DataTypes.STRING
}, {});
User.associate = function(models) {
// associations can be defined here
};
return User;
};
When I try to create it in my script file, I get this following error:
User.build is not a function
Here's how I call the build method:
const User = require('../models/User');
User.build({
username: message["name"],
}).save();
Upvotes: 0
Views: 1366
Reputation: 169
You need to require model from "model/index"
so change this
const User = require('../models/User');
to this
const {User} = require('../models/index');
refer to this answer
Upvotes: 0
Reputation: 6242
In your case it returns a function not constructor
you have to pass
sequelize
andDataTypes
while importing it
const User= require('../models/User')(sequelize, DataTypes);
Hope it'll work for you
Upvotes: 1