Mongoose : json value not updating

value is not updating

app.get("/api/exercise/log", (req, res) => {
  const userId = req.query.userId;
  console.log(userId);
  if (!userId) {
    res.send("enter user id");
  } else {
    userInfo.findById(userId, (err, data) => {
      if (err) {
        return err;
      } else {
        let a = data;
        a["etc"] = "etc";
        console.log(a);
        res.json(a);
      }
    });
  }
});

The above code cannot update sinnce you are just finding an item by id, use findAndUpdate instead… which takes a query i.e what to search and what to update

but I’m saving it in different variable

send me the link to your project
in the mean time, you could instead copy values in data to variable a like this:

let a = {...data}

that is if you want an object using the spread operator

I changed the code ,I’m using logProcessing function to give logs but its not working properly

fixed it , I was thinking log array was ["date1","date2"]
but it was [{date:date1},{date:date2}]

1 Like