1

It's update only false to true not working for true to false. I'm using node.js mongodb and express.js

This is My schema:

approve:{
  type: Boolean, default: false
}

index.js

.post('/update', function(req, res, next) {
    var id = req.body.id;
    var value = req.value;
    console.log(req.body);
    var update_stock = stmodel.findByIdAndUpdate(id, { $set: { approve: "value" } });
    update_stock.exec(function(err, doc) {
        if (err) throw err;
    });
Molda
  • 5,619
  • 2
  • 23
  • 39
Keju Patel
  • 31
  • 3
  • Why do you have quotes arround the value?? This `approve: "value"` will try to set `approve` to a string `value` . Just remove the quotes -> `{ $set: { approve: value } }` Also this `req.value` doesn't seem right, shouldn't it be `req.body.value`??? – Molda Sep 23 '20 at 06:21
  • but { $set: { approve: "value" } working for update false to true – Keju Patel Sep 23 '20 at 06:29
  • { $set: { approve: value } } does't working – Keju Patel Sep 23 '20 at 06:30
  • Does this answer your question? [How do I update/upsert a document in Mongoose?](https://stackoverflow.com/questions/7267102/how-do-i-update-upsert-a-document-in-mongoose) – Prathamesh More Sep 23 '20 at 07:21

1 Answers1

0

I think the problem is when you are trying to set the value to req.value, it should be req.body.value

var value = req.body.value;

Also, you have to make sure value is a Boolean, since it needs to be a Boolean according to your schema, or you can also handle it if it is a String or Boolean like this :

let approve = value === "true" ? true : ( value === "false" ? false : undefined);
stmodel.findByIdAndUpdate(id, { $set: { approve: approve } });
Ravi Shankar Bharti
  • 8,922
  • 5
  • 28
  • 52