1

I am new to node and here I am going to map mysql to my node project. model/User.js

const Sequelize = require('sequelize')
const db = require('../database/db.js')

module.exports = db.sequelize.define(
    'user',
    {
        user_id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        first_name: {
            type: Sequelize.STRING
        },
        last_name: {
            type: Sequelize.STRING
        },
        email: {
            type: Sequelize.STRING
        },
        password: {
            type: Sequelize.STRING
        }
    },
    {
        timestamps: false
    }
);
    

In the above code module.exports = db.sequelize.define() an error comes as db.sequelize.define is not a function. What is the reason for this error in code?

database/db.js

const Sequelize = require("sequelize")
const db = {}
const sequelize = new Sequelize("node","root","",{
    host: "localhost",
    dialect: "mysql",
    operatorsAliases: false,

    pool: {
        max: 5,
        min: 0,
        acquire:30000,
        idle:10000
    }
})

db.sequelize = sequelize
db.sequelize = Sequelize

module.exports = db
4
  • 1
    Please check the answer on stackoverflow.com/questions/49470508/… Commented Oct 10, 2020 at 17:10
  • 1
    Can you update the question and include the code of db.js? Commented Oct 10, 2020 at 23:43
  • 1
    @Runsis I edited the problem with db.js code. Commented Oct 11, 2020 at 2:46
  • 1
    OK, I found the problem and added an answer :D Kamalka! Hope you can mark it as accepted Commented Oct 11, 2020 at 4:50

2 Answers 2

2

The problem that you have is in your database file db.js in the way you are defining and exporting sequelize. I recommend you to change it to this:

  const { Sequelize } = require('sequelize');
    const db = new Sequelize("node","root","",{
        host: "localhost",
        dialect: "mysql",
        operatorsAliases: false,
    
        pool: {
            max: 5,
            min: 0,
            acquire:30000,
            idle:10000
        }
    })
    
    // I deleted what was causing the problem

    module.exports = db;

And in model/User.js you should just call define() with db.define():

const Sequelize = require('sequelize')
const db = require('../database/db.js')

// I created an object here so you can export it
const User = db.define(
    'user',
    {
        user_id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        first_name: {
            type: Sequelize.STRING
        },
        last_name: {
            type: Sequelize.STRING
        },
        email: {
            type: Sequelize.STRING
        },
        password: {
            type: Sequelize.STRING
        }
    },
    {
        timestamps: false
    }
);

module.exports = User;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your code. I really appreciate it and I learned a lot.
1
db.sequelize = sequelize
db.sequelize = Sequelize

This is what makes you the problems. remove the 2nd line. or change it to:

db.Sequelize = Sequelize

And then dont use

const Sequelize = require('sequelize')

on your file, use db.Sequelize instead.

1 Comment

Thank you. db.**S**equelize = Sequelize this was the problem with my code. when I correct it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.