I was looking at this other SO thread here: async/await with loop and wanted to do something similar with my code but unfortunately I can't see what the error is.
The groups
value here is an array taken from req.body.allGroups
. It doesn't seem to be performing any inserts at all into my my_groups
postgres table.
app.post("/create-group", async (req, res) => {
try {
const groups = req.body.allGroups;
console.log(groups);
for (const group of groups) {
console.log(group.groupName);
let newGroup = await pool.query(
"INSERT INTO my_groups (id, group_name) VALUES($1, $2) RETURNING group_id",
[ 99, group.groupName ]
);
res.json(newGroup.rows[0]);
}
} catch(err) {
console.error(err.messasge);
}
})
>>> UPDATED:
I have updated my code based on queries below and have also added console.log messages.
The console.log output after running the above updated code is as follows:
[
{
groupName: 'G1'
},
{
groupName: 'G2'
}
]
G1
undefined