tediousjs / node-mssql

Microsoft SQL Server client for Node.js
https://tediousjs.github.io/node-mssql
MIT License
2.23k stars 469 forks source link

Cannot read property 'recordsets' of undefined ! UNhandle promise Rejection code 20. mssql nodejs #704

Closed Raja0sama closed 6 years ago

Raja0sama commented 6 years ago

I am trying to make an API, I already have but currently, I cannot connect it to multi-users you know like it doesn't give response some and stuff so I decided to switch it to pool which correct me if I am wrong handled multiconnection at the same instance. HERE IS the handler

`app.use(bodyParser.json());

//CORS Middleware
app.use(function (req, res, next) {
    //Enabling CORS 
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
    next();
});
  // 

//Initiallising connection string

//Initiallising connection string
const pool = new sql.ConnectionPool({
  user: 'sa',
    password: "p3",
    server: 'somehting',
    database: 'CUBasES',
encrypt: false
});

var con = pool;

function executeQuery(query) {
return new Promise((resolve, reject) => {
conn.then(function (){
    var req = new sql.Request(conn);
    req.query(query).then(function (recordset) {
        resolve(recordset);

    conn.close()  
    })
        .catch(function (err) {
            reject(err);
    conn.close()

        })
})
    .catch(function (err) {
        console.log(err);
    });
});    
}`

and here is the request

app.get("/lastbill", function (req, res) {
    var query = "SELECT TOP (1)       [bill_hdr_key]      ,[bill_sale_date]      ,[bill_no]        ,[bill_amt]      ,[bill_disc_amt]      ,[bill_paid_amt]            ,[inv_loc_key]           ,[Payment_type_desc]          ,[inv_loc_name]      ,[payment_key]  FROM [CUBES].[dbo].[vw_bill_summary] order by bill_sale_date desc";
    executeQuery(query)
        .then((data) => {
            res.status(200).send({ "msg": "Records fetched", "data": data.recordsets });
        }).catch((err) => {
            res.status(500).json({ "msg": err.message }); coni.close();
        });
});

and the error i am facing on display is this {"msg":"Cannot read property 'recordsets' of undefined"}

and in console is this

0|server   | { ConnectionError: Connection is closed.
0|server   |     at Request._query (C:\Users\HOfrn\node_modules\mssql\lib\base.j
s:1299:37)
0|server   |     at Request._query (C:\Users\HOfrn\node_modules\mssql\lib\tediou
s.js:497:11)
0|server   |     at PromiseLibrary (C:\Users\HOfrn\node_modules\mssql\lib\base.j
s:1265:12)
0|server   |     at new Promise (<anonymous>)
0|server   |     at Request.query (C:\Users\HOfrn\node_modules\mssql\lib\base.js
:1264:12)
0|server   |     at Promise (C:\Users\HOfrn\node_modules\windows\server.js:38:9)

0|server   |     at new Promise (<anonymous>)
0|server   |     at executeQuery (C:\Users\HOfrn\node_modules\windows\server.js:
35:8)
0|server   |     at C:\Users\HOfrn\node_modules\windows\server.js:86:5
0|server   |     at Layer.handle [as handle_request] (C:\Users\HOfrn\node_module
s\express\lib\router\layer.js:95:5) code: 'ECONNCLOSED', name: 'ConnectionError'
 }
0|server   | (node:6224) UnhandledPromiseRejectionWarning: ReferenceError: coni
is not defined
0|server   |     at executeQuery.then.catch (C:\Users\HOfrn\node_modules\windows
\server.js:90:59)
0|server   |     at <anonymous>
0|server   | (node:6224) UnhandledPromiseRejectionWarning: Unhandled promise rej
ection. This error originated either by throwing inside of an async function wit
hout a catch block, or by rejecting a promise which was not handled with .catch(
). (rejection id: 22)

Kindly help me out i am stuck on this from ages. I have tried every thing but not able to make it work .

willmorgan commented 6 years ago

You are closing the entire pool at the end of executeQuery.