0

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?

3
  • OK! Probably await client.end() is not waiting for rows.forEach will finish executing. You have to change rows.forEach to i.e. for loop. Or you can also to do something like that: stackoverflow.com/a/51738717/8890700 - it is map all of your rows into array of Promises and call await Promise.all(); before await client.end(). Commented Jun 1, 2022 at 19:58
  • 1
    Does this answer your question? Using async/await with a forEach loop Commented Jun 1, 2022 at 20:05
  • The canonical solution for that is to use a database cursor for the outer query. Commented Jun 2, 2022 at 6:20

2 Answers 2

0

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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.

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.