0

I'm new to sequelize and trying to set it up for my new project. I checked some answers on this, but couldnt get past my error. Can someone point out how to fix this.

models/index.js

// Database service
// Connects to the database
const { Sequelize } = require('sequelize');
const path = require('path');


const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
    host: process.env.DB_HOST,
    dialect: 'mysql',
    logging: process.env.QUERY_LOGGING == "true" ? console.log : false,
    pool: {
        max: 10,
        min: 0,
        acquire: 30000,
        idle: 10000
      }
});


module.exports = sequelize

models/users.js

const sequelize = require("./index")
module.exports = (sequelize, DataTypes) => {
    const User = sequelize.define('Users', {
        id: {
            type: DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
          },
        firstName: {
             type: DataTypes.STRING,
              allowNull: false
         },
        lastName: {
             type: DataTypes.STRING
        },
        profileURL: {
             type: DataTypes.STRING
        },
        emailId: {
             type: DataTypes.STRING,
             allowNull: false 
        },
        passwordHash: {
             type: DataTypes.STRING,
            allowNull: false
        },
        street: {
             type: DataTypes.STRING
        },
        city: {
             type: DataTypes.STRING,
             allowNull: false 
        },
        phone: {
             type: DataTypes.STRING 
        },
        newsletter: {
             type: DataTypes.STRING
         },
        visibility: {
             type: DataTypes.BOOLEAN,
             defaultValue: true
        },

    },{
    });
    return User;
};

And finally, I'm importing the User model in my service file like below:


const User = require("../models/users")

const createUser = async(req) => {
    const {firstName, lastName, profileURL, emailId, passwordHash, street, city, phone, newsletter, visibility} = req.body
    const user = await User.create({
        firstName, 
        lastName,
        profileURL, 
        emailId,
        passwordHash,
        street,
        city,
        phone,
        newsletter,
        visibility     
    })
    console.log("new user==>>", user)
    return
}

module.exports = { createUser }

However, I get the following error.

TypeError: User.create is not a function

Can someone point out what I could be doing wrong? I realize it could be something minor.

Thank you

sparklyCode
  • 149
  • 1
  • 1
  • 7

1 Answers1

1

You export a function that registers the User model and not the model itself. So you just need to call it passing sequelize instance and DataTypes somewhere like database.js where you will register all models and their associations or directly in models/index.js:

const UserModelConstructor = require("../models/users")
const { DataTypes } = require("sequelize");
...
const UserModel = UserModelConstructor(sequelize, DataTypes);

module.exports = {
  sequelize,
  User: UserModel
}

You can look at how to register multiple models and association in my other answer here

Please don't forget to remove this line

const sequelize = require("./index")

from models/users.js

Anatoly
  • 20,799
  • 3
  • 28
  • 42