Search code examples
sequelize.jses6-promise

Can't resolve promise from sequelize.query


This will no doubt be embarrassing, but can anyone tell me why the promise from the simple raw query below doesn't resolve? It's sequelize 4.37.10.

Thanks

'use strict';

const Sequelize = require('sequelize');

const options = {
    dialect: 'postgres',
    host: 'localhost',
    database: 'transitdispatch',
    username: 'postgres',
    password: 'postgres'
};

const sequelize = new Sequelize(options);
const p = new Sequelize.Promise(function(resolve) {
    return sequelize
        .query('SELECT * FROM "Schedule";', {type: sequelize.QueryTypes.SELECT})
        .then((schedules) => {
            return resolve(schedules);
        })
        .catch((error) => {
            console.log(error);
        });
});

p.then((schedules) => {
    console.log(`there were ${schedules.length} schedules`);
});

Solution

  • The problem is not Promise. Node process will exit automatically when there is nothing to do. In this case, if you close the sequelize connection, it should exit

    p.then((schedules) => {
        console.log(`there were ${schedules.length} schedules`);
        sequelize.close();
    });
    

    You can also use process.exit() if you are sure you want to exit even if there is something else pending. You can use

    process._getActiveHandles();
    process._getActiveRequests();
    

    to figure out what is holding your node process to exit.