1

I am new to node js. I know about event loop that node waits if there is any callbacks or events still pending. I have written a code to connect db and fetch data. The node doesn't exit even after the data is read.

var sql = require("mssql");
var config = {
    user: 'data',
    password: 'abc@123',
    server: 'localhost', 
    database: 'master' 
};
sql.connect(config, function (err) {
    var request = new sql.Request();
    var i;
    request.query('GetData').then(
        ri=>{
          console.log(ri.recordset.map(o=>o.Key));
        })

})

Can any one please help me why this is happening?

1
  • Node is not closing because sql.connext creates a active connection, so in the eyes of node a event listener is registered. Maybe try sql.close() Commented Sep 23, 2019 at 10:02

1 Answer 1

1

You can close the connection after your query is complete, this will allow the process to exit:

var sql = require("mssql");
var config = {
    user: 'data',
    password: 'abc@123',
    server: 'localhost', 
    database: 'master' 
};

sql.connect(config).then(pool => {
    var request = new sql.Request();
    request.query('GetData;').then(ri => {
        console.log(ri.recordset.map(o => o.Key));
        // Close the pool now we're done with it...
        pool.close();
    });
}).catch (err => {
    console.error("An error occurred:", err);
});
Sign up to request clarification or add additional context in comments.

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.