1

I am trying to connect SQL database from NodeJs, but getting this error: Error while querying database :- ConnectionError: Connection not yet open.

 var executeQuery = function(res, query) { 
   var conn = new sql.ConnectionPool(dbConfig);

   conn.connect().then(function () {
     // create Request object
     var request = new sql.Request(conn);
     // query to the database
     request.query(query, function (err, queryResult) {
       if (err) {
         console.log("Error while querying database :- " + err);
         res.send(err);
       } else {
         res.send(queryResult);
       }
     });

     conn.close();
   })
}

Please help.

Thank You James

2
  • Can you include your require statement sql = require("...?!?");? Commented Aug 11, 2017 at 13:26
  • @kumbhanibhavesh the error i get is ConnectionError: Connection not yet open. Commented Aug 11, 2017 at 14:33

1 Answer 1

1

The connection pool class will return a promise, which resolves with the pool that was initiated by your connection :

 var executeQuery = function(res, query) { 

     var conn = new sql.ConnectionPool(dbConfig);

     conn.connect()
         .then(function (pool) {
             //    ^ the pool that is created and should be used

             // create Request object
             var request = new sql.Request(pool);
             //                            ^ the pool from the promise

             // query to the database
             request.query(query, function (err, queryResult) {
                 if (err) {
                     console.log("Error while querying database :- " + err);
                     res.send(err);
                 } else {
                     res.send(queryResult);
                 }
             });
             conn.close();
        });           
}

In your case conn variable is always the promise and not the connection itself.

A basic example of a connection pool from the documentation is as follows :

new sql.ConnectionPool(config).connect().then(pool => {
    return pool.query`select * from mytable where id = ${value}`
}).then(result => {
    console.dir(result)
}).catch(err => {
    // ... error checks
})
Sign up to request clarification or add additional context in comments.

5 Comments

@drichev Hi Can you please elaborate? My require statement is : sql = require("mssql"); You mean , I should be creating a DB pool? Could you please show that for me?
Well as far as what you pasted you want to create a connection pool and use it to make the query and the request. There are two ways to make it. 1 with a Promise and 2 with a callback. When you use it with a Promise ( no callback function provided ) you actually need to take what the promise resolved result is as your pool variable.
If possible could you please edit the same in my code to show the same? I am new to this, so not able tp grasp much.
Edited my answer.
thanks @drichev... but I am still getting the error like : ConnectionError: Connection not yet open. Mycurrent code is like exatly like your answer above. Kindly do help

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.