Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

nodejs express PUT error and number shows in every users profile

Help please

This is how I write the code

PUT requested in 1 user

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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');
  }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading