3

I have an async function that I am using to query mysql database in Node.js. I am waiting for the result of the query execution and inserting the result into an array.

  async function getOpenOrders() {
    try {
        const arrayLoads = [],
            last4Orders = await pool.query( `${query1} ` )
        // console.log(last4Orders)
        for ( let i = 0; i < last4Orders.length; i++ ) {
            // console.log(last4Orders[i].orderID)
            console.log( arrayLoads )
            console.log( `===================` )
            const rows = await pool.query( `${query2}` )
            console.log( rows )
            arrayLoads.push( rows )
            // console.log(arrayLoads)
        }
        // console.log(rows[0])
        // console.log(arrayLoads)
        res.send( arrayLoads )
    } catch ( err ) {
        console.log( err )
    }
}

getOpenOrders()

However, the await query inside the for loop is not stopping the sync code because my console.log() displays something like:

    []
    ===================
    []
    [ [] ]
    ===================
    []
    [ [], [] ]
    ===================
    [RowDataPacket { Number: '4732', Test: 'TQLM-384', User: 23 },
      RowDataPacket { Number: '4732', Test: 'TQLM-384', User: 23 }]
[ [],
  [],
[RowDataPacket { Number: '4732', Test: 'TQLM-384', User: 23 },
      RowDataPacket { Number: '4732', Test: 'TQLM-384', User: 23 }]
===================
[RowDataPacket { Number: '4732', Test: 'TQLM-384', User: 23 },
      RowDataPacket { Number: '4732', Test: 'TQLM-384', User: 23 }]

1 Answer 1

3

mysql pool.query method returns a callback, if you want to use async await, try to promisify the method and then use async await

 const {promisify} = require('util');
    async function getOpenOrders() {
        try {
            const arrayLoads = [],
                query = promisify(pool.query).bind(pool);
                last4Orders = await query( `${query1} ` )
            // console.log(last4Orders)
            for ( let i = 0; i < last4Orders.length; i++ ) {
                // console.log(last4Orders[i].orderID)
                console.log( arrayLoads )
                console.log( `===================` )
                const rows = await query( `${query2}` )
                console.log( rows )
                arrayLoads.push( rows )
                // console.log(arrayLoads)
            }
            // console.log(rows[0])
            // console.log(arrayLoads)
            res.send( arrayLoads )
        } catch ( err ) {
            console.log( err )
        }
    }

    getOpenOrders()
Sign up to request clarification or add additional context in comments.

1 Comment

My pool is already promisified. My issue was that the result of 2 out of 4 queries resulted in an empty array and I thought that the await code was not working.

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.