My goal is to update a specific location only if it has a status of 0 or 2, but not if the status is 1. There is only one instance of this location in my database.
Property.findOneAndUpdate({ status: 0, location: req.body.update.location }, req.body.update, err => {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true });
});
Property.findOneAndUpdate({ status: 2, location: req.body.update.location }, req.body.update, err => {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true });
});
However, I noticed that the code above updates the property even when the status is 1.
Property.find({location: req.body.update.location}, (err, Propertyz) => {
myProperty = Propertyz
console.log(myProperty[0].status)
if(myProperty[0].status != 1) { // returns and doesn't update if true
console.log("updating")
Property.findOneAndUpdate({ location: req.body.update.location }, req.body.update, err => {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true });
});
}
else {
console.log("rejecting")
return res.json({ success: true });
}
})
I made some changes and now it works fine, although I still don't know why the previous version wasn't working as expected. I also wonder if there's a way to combine the two separate functions into one for efficiency.