Monday, 22 May 2017

Node express app calling mssql is saying that Connection is closed

I have another app which uses express and routes but this new app i was slimming it down. I know the connection string stuff is correct

script.getQuestions(connection);

script.getQuestions = function(connection,req, res){
   console.log(connection);
}

I have read that some people said online to change to use a promise for async fixes this... problem is that with my function having req and res i don't

"ConnectionError: Connection is closed"

"(module.js:487:32) code: 'ECONNCLOSED', name: 'ConnectionError' }"

What I call up (script) is

var sql = require('mssql');

exports.getQuestions = function(connection, req,res){
 console.log(connection);
var request = new sql.Request(connection);

var query = 'select * from Question'
request.query(query).then(function(resultset){
    res.json(resultset.recordset);
}).catch(function(err){
    console.log(err);
    //res.json(err)
})
}



via Jeremy Miller