Home > Software design >  User.create is not a function (Sequelize Error)
User.create is not a function (Sequelize Error)

Time:09-20

I successfully created migrations using sequelize cli, but now when trying to make request in postman I'm getting the error:

User.create is not a function 

I don't know what's wrong with my application's model, I also tried changing the import on the page to

const { Customer } = require('../../models/customer');

rather than

const Customer = require('../../models/customer');

But in this case I get the error

TypeError: Cannot read properties of undefined (reading 'create')

Here the customer model

'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Customer extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
    }
  }
  Customer.init({
    name: DataTypes.STRING,
    email: DataTypes.STRING,
    password: DataTypes.STRING,
    country: DataTypes.STRING,
    city: DataTypes.STRING,
    state: DataTypes.STRING,
    address: DataTypes.STRING,
    postalCode: DataTypes.STRING
  }, {
    sequelize,
    modelName: 'Customer',
  });
  return Customer;
};

index model

'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);
    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;

controller

const {Customer} = require('../../models/customer');

module.exports = {
    async store (req, res) {
        try {
            const { name, email, password, country, city, state, address, postalCode } = req.body;

            const customer = await Customer.create({ name, email, password, country, city, state, address, postalCode });
            
            return res.status(200).json(customer);
        } catch (err) {
            return console.log(err);
        }
    }
}

CodePudding user response:

This import

const Customer = require('../../models/customer');

is just a function to register Customer model. You already have all models as registered in index module. You just need to use them from index module:

const db = require('../../models/index');

module.exports = {
    async store (req, res) {
        try {
            const { name, email, password, country, city, state, address, postalCode } = req.body;

            const customer = await db.Customer.create({ name, email, password, country, city, state, address, postalCode });
            
            return res.status(200).json(customer);
        } catch (err) {
            return console.log(err);
        }
    }
}
  • Related