Home > Blockchain >  Unable to execute another query while iterating the first query
Unable to execute another query while iterating the first query

Time:06-02

I need to execute another query while iterating the first query as follows, but I keep getting an error "const error = this._ending ? new Error('Connection terminated') : new Error('Connection terminated unexpectedly')". The query is not a final query. I am just testing it out.

const client = new Client(credentials)
await client.connect()

const userQuery = await client.query('select c.* from clients c')
const rows = userQuery.rows
rows.forEach(async row => {
    console.log("user", row)
    const orderQuery = await client.query(`select o.created from orders o where o."userId" = ${row.id} order by o.created desc limit 1`)
    const orderRows = orderQuery.rows
    orderRows.forEach(order => {
        console.log(order)
    })
})
await client.end()

is there a way to execute another query while iterating the first query?

CodePudding user response:

Your problem is that the rows.forEach is not async. The interpreter does not wait for the forEach to finish before closing the connection await client.end(). Try to change row.forEach to an async function such as:

async function processRowsData(rows) {
    for(let row of rows) {
        console.log("user", row);
        const orderQuery = await client.query(`select o.created from orders o where o."userId" = ${row.id} order by o.created desc limit 1`)
        const orderRows = orderQuery.rows;
        for(let order of orderRows) {
            console.log(order);
        }
    }
}

async function doSomethingInParallel(query) {
    const client = new Client(credentials);
    await client.connect();

    const userQuery = await client.query(query);
    const rows = userQuery.rows;
    await processRowsData(rows);
    await client.end();
}

(async () => {
    doSomethingInParallel('select c.* from clients c');
    doSomethingInParallel('select c.* from clients c');
})();

With this solution you will be able to run multiple queries at the same time and fix your connection issue.

CodePudding user response:

You are not awaiting the queries you are making while iterating over the rows, causing you to end your connection before they are done. You need to wait for it to finish by using

await Promise.all(rows.map(async () => {
  ...
})
await client.end()
  • Related