user9132502
user9132502

Reputation: 385

Sequelize build is not a function when model is created from migration

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

Answers (2)

Arfan
Arfan

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

abdulbari
abdulbari

Reputation: 6242

In your case it returns a function not constructor

you have to pass sequelize and DataTypes while importing it

const User= require('../models/User')(sequelize, DataTypes);

Hope it'll work for you

Upvotes: 1

Related Questions