Saturday, 25 July 2020

Using async await in Nodejs mssql package

I am using `node-mssql` package and wanted to export a connection pool that I can call in various routes.database.jsconst sql = require('mssql/msnodesqlv8'); const keys = require('../config/keys'); const util = require('util') //Need this package for windows authentication require('msnodesqlv8'); let config; console.log(keys) if (keys.db.state.toLowerCase() == "dev") { config = { user: keys.db.dev_user, password: keys.db.dev_password, server: keys.db.dev_server, // You can use 'localhost\\instance' to connect to named instance database: keys.db.dev_database, // driver:'msnodesqlv8', pool: { max: 10, min: 5, idleTimeoutMillis: 30000 }, options: { encrypt: false, instanceName: keys.db.dev_instanceName, enableArithAbort: true }, // port:57909, enableArithAbort: false } } console.log(config) let pool = new sql.ConnectionPool(config).connect() .then(query => { return query }) .catch(e => console.error("Database Trouble! ", e)) module.exports = pool; In one of my routes:const pool=require('../middleware/database') async function queryDatabase(){ try{ let rows = await pool let result = await rows.query(`Select * from table1`) let result2 = await rows.query(`Select * from table2`) } catch(err){ console.log(err) } } My code is working just fine and I am getting the results. But I really want to be able to dolet result = await pool.query(`Select * from table1`) directly without the line :let rows = await pool but when I do that I get an error:pool.query is not a function.I cannot figure out what is going on.

Submitted July 26, 2020 at 12:15AM by sahilgreen

No comments:

Post a Comment