nodejs express PUT error and number shows in every users profile

Issue

Help please

This is how I write the code

PUT requested in 1 user

Now showing that on both users

I am trying to increase each user’s "entries" number by doing PUT req. But whenever I do that for only 1 user. the number automatically increases in both users’ "entries". You can understand better when you see the picture.

Thank you so much

Solution

In here you increasing user’s entry for all the users. If you want to increase only for a specific user add the increase logic inside the if condition.

For example like below

app.put('/image', (req, res) => {
  const { id } = req.body;

  let found = false;

  database.users.forEach(users => {
    if (users.id === id) {
      found = true;
      users.entries++
      return res.json(users.entries);
    }
  });
  if (!found) {
    res.status(400).json('not found');
  }
});

Answered By – Kokulan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published