express - Node js: mssql [ConnectionError: Connection is closed.] name: 'ConnectionError', message: 'Connection is closed.', code: 'ECONNCLOSED' -


i getting error in npm mssql 3.0.0 sqlserver 2012

i creating single page application used restful using express . there 4 method executing query , returning data response. each method opening connection , closing connection.

but when savedquery calling connection close error occurs.

each method code similar savedquery method (copy pasted code queries changed) executing savedquery not executing

{ [connectionerror: connection closed.] name: 'connectionerror', message: 'connection closed.', code: 'econnclosed' }

var savedquery=function(req,res){        dbconfig= {                 user: 'xxx',                 password: 'xxxxxxxxxx',                 server: 'localhost', // can use 'localhost\\instance' connect named instance                  database: 'demo_ods',                        options: {                     encrypt: true                 }             };          sql.connect(dbconfig).then(function (err) {                 var sqlrequest = new sql.request();                 sqlrequest.query("select * savedquery").then(function (recordset) {                     sql.close(function (value) {                       console.log("connection6 closed");                     });                     return res.status(200).send(recordset);                  }).catch(function (err) {                     console.log(err);                 });             }).catch(function (err) {                  console.log(err);             });         }; } 

i know old questionm answer others facing same isue. had same problem, did is, used promises below.

    function getdata() {     try {         sqlinstance.connect(setup)             .then(function () {                 // function retrieve data - start                 new sqlinstance.request()                     .query("select * course")                     .then(function (dbdata) {                         if (dbdata == null || dbdata.length === 0)                             return;                         console.dir('all courses');                         console.dir(dbdata);                     })                     .catch(function (error) {                         console.dir(error);                     });                  // function retrieve data - end                  // retrieve specicfic data - start                 var value = 1;                 new sqlinstance.request()                     .input("param", sqlinstance.int, value)                     .query("select * course courseid = @param")                     .then(function (dbdata) {                         if (dbdata == null || dbdata.length === 0)                             return;                         console.dir('course id = 1');                         console.dir(dbdata);                     })                     .catch(function (error) {                         console.dir(error);                     });                 // retrieve specicfic data - end              }).catch(function (error) {                 console.dir(error);             });     } catch (error) {         console.dir(error);     } } 

this solved issue. can find fix here.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -