JavaScript update value in mongodb atlas with axios not work

Viewed 18

I'm trying to update value in mongoDB and its not changing the value , checked the values and they are ok . and i trying to see errors and everything is good . what I'm missing ?

Mongo atlas picture of my mongo values

type: Number,

default: 0

code :

// symbol = the symbol of the coin | ammount = the ammount of coin
// in this case : symbol = USDT | ammount = 3
  const setCoin = async (symbol, ammount) => {
    setError(false);
    try {
      setError(false);
      const res = await axios.patch("http://localhost:3001/api/users/setCoin", {
        email: user.email,
        symbol, ammount
      });
      res.data && window.location.replace("/home");
      console.log(error)
    } catch (err) {
      setError(true);
    }
  };

server side

//setCoin

router.route("/setCoin")
  .patch(async (req, res,) => {
    try {
      const symbol = req.body.symbol;
      const user = await User.findOneAndUpdate({ "email": req.body.email }, { $set: { symbol: req.body.ammount } }, { upsert: true });
      console.log("succes to set coin");
    } catch (err) {
      res.status(500).json(User.symbol);
      console.log(err)
    }
  });
1 Answers

So as I dont know how you defined your User model can you try the following:

const user = await User.findOneAndUpdate({ "email": req.body.email }, { $set: { [symbol]: req.body.ammount } }, { upsert: true });
Related