I am trying to get some data from the database with async/await then create an object with some of the data and insert into the database. I need the calls to be synchronous for this in order to send complete data for insertion.
I have an async function but the await is not working...
The code is below:
async function queryDb(roleName) {
let pool = await sql.connect(dbConfig);
let data = await pool.request()
.query(`select id from table where name='${roleName}'`);
roleId = data.recordset[0].id;
pool.close;
sql.close;
return roleId;
}
queryDb(roleId)
.then(result => {
console.log(result);
})
.catch(err => {
pool.close;
sql.close;
console.log(err)
})
async function program() {
const xlsxFile = require('read-excel-file/node');
xlsxFile('./file.xlsx').then((rows) => {
let roleid = await queryDb(rows[0][1]); // here is not working
let insretObj = {
field1: rows[0][0],
field2: rows[1][1],
field3: rows[2][2],
field4: rows[3][3],
field5: rows[4][4],
field6: rows[5][5],
role_id: roleid,
};
assignRoles(insretObj);
});
};
program().then(console.log('hereeeeeee'));
I really appreciate your answer.
pool.closeandsql.close? Are those supposed to be function calls that you actually call as inpool.close()andsql.close()?pool.closedoes nothing.