0

say i have the following modelConfiguration.js file:

const Sequelize = require('sequelize');
const dbConfig = require('../config/database.config');
module.exports = function () {

    const sequelize = new Sequelize(dbConfig.database, dbConfig.user, dbConfig.password, {
        host: 'localhost',
        dialect: 'mysql',

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

        // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators
        operatorsAliases: false
    });


    const employee = sequelize.define('employee', {
        id:{
            type: Sequelize.DataTypes.INTEGER,
            primaryKey: true
        },
        token: Sequelize.DataTypes.TEXT
    });

    return sequelize;


};

Now here as you can see i return the sequelize object.

Now i wish to set a variable equal to the object returned so in my server.js i do the following:

const seq = require('./models/modelConfig');

However this does not work.

Can anyone tell me what ive done wrong? (Maybe ive misunderstood something)

3
  • 3
    Your module.exports returns a function that when called returns that Sequelize instance. Commented Dec 19, 2018 at 12:36
  • 2
    const seq = require('./models/modelConfig')(); This is what you should do. Commented Dec 19, 2018 at 12:36
  • Yup, get rid of the function and just return sequelize Commented Dec 19, 2018 at 12:37

2 Answers 2

1

Calling require('./models/modelConfig'); returns a function that initializes Sequelize.

You should call the method to get an instance:

const seqInitializer = require('./models/modelConfig');
const seq = seqInitializer();

Alternatively. Just return sequelize directly:

const Sequelize = require('sequelize');
const dbConfig = require('../config/database.config');
function initSequelize() {

    const sequelize = new Sequelize(dbConfig.database, dbConfig.user, dbConfig.password, {
        host: 'localhost',
        dialect: 'mysql',

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

        // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators
        operatorsAliases: false
    });


    const employee = sequelize.define('employee', {
        id:{
            type: Sequelize.DataTypes.INTEGER,
            primaryKey: true
        },
        token: Sequelize.DataTypes.TEXT
    });

    return sequelize;


};
module.exports = initSequelize();
Sign up to request clarification or add additional context in comments.

Comments

1

Just Try to add this.. It will give u back an object instead of function

const seq = require('./models/modelConfig')();

Comments

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.