I need update postgresql table records in loop. I write this code:
const { Pool } = require('pg');
var slug = require('slug');
const credentials = {
user: 'postgres',
host: 'localhost',
database: 'dev',
password: 'secret',
port: 5432,
};
const pool = new Pool(credentials);
async function getEditions() {
const sql = `SELECT * FROM editions`;
return pool.query(sql);
}
async function updateEdition(id, slug) {
const values = [id, slug];
try {
const text = `UPDATE editions SET slug = $2 WHERE id = $1`;
await pool.query(text, values);
console.log('Updated');
} catch (error) {
console.error(error);
}
}
(async () => {
const getEditionsResult = await getEditions();
getEditionsResult.rows.forEach(async (e) => {
await updateEdition(e.id, slug(e.name));
});
await pool.end();
})();
But data not updated in forEach loop. If i try update one record, it's ok.
What am I doing wrong?