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?
await client.end()is not waiting forrows.forEachwill finish executing. You have to changerows.forEachto i.e.forloop. Or you can also to do something like that: stackoverflow.com/a/51738717/8890700 - it ismapall of your rows into array ofPromises and callawait Promise.all();beforeawait client.end().