0

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
12
  • 1
    Are you sure req.body.allGroups contains a Promise? It's not impossible but quite unusual to see await on a property rather than on a function. Commented May 27, 2021 at 10:52
  • When you set res.json doesn’t that send a response back to the user, effectively ending the loop after one iteration Commented May 27, 2021 at 10:56
  • What's the actual problem? That pool.query("INSERT ...") doesn't create new entries? Then why the title? Commented May 27, 2021 at 10:56
  • 2
    @James It doesn't end the loop. It just causes exceptions from the second iteration onwards. Commented May 27, 2021 at 11:01
  • 1
    @tonyf Collect them into an array, respond with the array after the loop? I don't know what you're trying to achieve. Commented May 27, 2021 at 12:24

3 Answers 3

1
  1. you shouldnt do res.json() in the for loop. try putting it one ligne below, and adapt it, as the newGroup variable wont be reachable.

  2. await req.body.allGroups is not usual. unless your library needs this await should not be used.

  3. anyway, can you try to console.log(groups) before the loop to confirm that the data exist please. If yes, you have a problem in you pool.

  4. the catch part should do something like res.send('Error') or better : next(err); if you don't, the browser connexion will stay hanging.

Sign up to request clarification or add additional context in comments.

2 Comments

Have updated my post and also provided sample output. Hope this helps
Could you please assist of where to place res.json() as outside of the loop, the newGroup variable is not defined?
0

Some output would've been helpful. But i suggest you to debug the value of groups, i believe it stays empty, so the loop never runs, that's why nothing happens and you may not be getting may error. Also, await is supposed to be put in front of promise for which we want to wait. So make sure req.body.allGroups is a promise.

1 Comment

Have updated my post and also provided sample output.
0

By just console logging the whole err I managed to track that the issue was in fact a foreign key constraint violation that I have on the my_groups table.

All good now.

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.