I am connecting SQL Server using NodeJS. My initial code was:
const poolA = new sql.ConnectionPool(config, err => {
poolA.request()
.query("select * from AnyTable", function(err, result) {
if (err)
message = "Error!";
else {
//Do something else
message = "Done!";
}
})
});
I was getting "connection s closed error". I included
poolA.close()
and it didn't solve the problem either.
I changed this to:
new sql.ConnectionPool(config).then(pool => {
pool.request()
.query("select * from AnyTable")
}).then(result => {
//Do something else
message = "Done!";
sql.close();
}).catch(err => {
message = "Error!";
sql.close();
});
and now I get ".then is not a function" error.
What is the correct way to:
- Create a connection if it doesn't exist
- If connection already exist, use it
I am getting all sorts of error. Can someone help me sort out this problem?