1

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.

9
  • What library are you using for your sql access? Does it support promises? Commented Dec 11, 2020 at 19:34
  • Also, what is the point of pool.close and sql.close? Are those supposed to be function calls that you actually call as in pool.close() and sql.close()? Commented Dec 11, 2020 at 19:35
  • @jfriend00 I am using mssql. and regarding the other one yes I suppose they are the same. Commented Dec 11, 2020 at 19:37
  • They are not the same. A statement like pool.close does nothing. Commented Dec 11, 2020 at 19:38
  • @jfriend00 is this related to my problem? the await is not working inside program function Commented Dec 11, 2020 at 19:41

1 Answer 1

3

it shows this error "await is only valid in async function". I except to get the roleId here and create the object below with that role id.

Your use of await is inside the nested .then() handler callback which is not itself async.

In general, you don't want to mix .then() and await within the same control-flow. I would suggest you change it to this:

const xlsxFile = require('read-excel-file/node');

async function program() {
    let rows = await xlsxFile('./file.xlsx');
    let roleid = await queryDb(rows[0][1]);
    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);
}
Sign up to request clarification or add additional context in comments.

Comments

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.