Search code examples
node.jssql-serversql-server-2008node-mssql

pool.request() is not a function


enter code here

**my dbconnection.js**  

  const sql = require("mssql");
const config = {
    user: 'sa',
    password: 'abhi',
    server: 'ABHI\\MSSQLSERVER01', 
    database: 'online_video_streaming' 
};
const poolPromise =new sql.ConnectionPool(config)
    .connect()
     .then (pool =>{
        console.log("connected")
            return pool.request()
        }).catch(err => console.log("failed to connect", err));
console.log("connection", poolPromise)
  module.exports = {
                    poolPromise:poolPromise,
                    sql:sql
                };

**my users.js**
const {sql,poolPromise} = require('../../dbconnection');
router.post('/getcatlist', async (req, res) => {
  try{
    const pool = await poolPromise;
    const request = await pool.request();
  let userquery = 'SELECT  * FROM cat_1_level_1';
  let response = await request.query(userquery);
  res.send("200",response);
  console.log(response);
}catch (e){
  console.error(e)
}
});

output: tedious deprecated The default value for config.options.enableArithAbort will change from false to true in the next major version of tedious. Set the value to true or false explicitly to silence this message. node_modules\mssql\lib\tedious\connection-pool.js:61:23

connection Promise { pending } connected TypeError: pool.request is not a function at C:\Users\ABHILASH M\Desktop\nodejs\Online_video_streaming2\routes\portal\manage\user.js:430:32 at processTicksAndRejections (internal/process/task_queues.js:97:5)


Solution

  • You are returning pool.request() in db.js.

    Do this instead:

    const poolPromise =new sql.ConnectionPool(config)
        .connect()
        .then (pool =>{
            console.log("connected")
            return pool
        })
        .catch(err => console.log("failed to connect", err)); 
    

    Also, you started well with const, but then you started declaring things as mutable that never get mutated. Change those let statements to const and you'll keep on #winning. See this article.