0

i am working on an Ionic-1 + nodejs + angular application. My mongoDb findOneAndUpdate() function returns true on each call even the first call updates database. nodejs:

app.post('/booking', function (req, res) {

var collection = req.db.get('restaurant');
var id = req.body.id;
var status = req.body.status;
collection.findOneAndUpdate({status: status, id: id},{$set:{status:"booked"}}, function (e, doc) {
    console.log(id, status);
    if (e) {
        console.log(e);
    }
     else if(!doc) {
        res.send(false);
    }
     else {
        res.send(true);
    }
});
});

controller.js

$scope.bookMe = function(id){
    var Obj = {status: "yes", id: id};
    myService.booking(Obj).success(function(res){
        console.log(Obj, "Checking  status")
        console.log(res);
        if (res == true) {
            var alertPopup = $ionicPopup.alert({
            title: 'Booking Confirm',
            template: 'Thanks For Booking'
            });
    }
    else{
        var alertPopup = $ionicPopup.alert({
            title: 'Error',
            template: ' Not available'
            });
    }
    })

};

where i am doing wrong. my DB gets updated but it returns true always on next call.

Najam Us Saqib
  • 3,190
  • 1
  • 24
  • 43

2 Answers2

0

The documentation about findOneAndUpdate says :

Finds a matching document, updates it according to the update arg, passing any options, and returns the found document (if any) to the callback. The query executes immediately if callback is passed.

So it's regular behavior you got a doc.

Orelsanpls
  • 22,456
  • 6
  • 42
  • 69
  • so which function should i use to update and check ? – Najam Us Saqib Sep 05 '17 at 16:07
  • I don't get your problem here. If the data do not exist in database, `doc` is false. If the data exists, it `update` it and `doc` equals the data. (if you want the data to get created in case it do not exists, set `upsert` option to true) – Orelsanpls Sep 05 '17 at 16:09
  • problem is if a user books a restaurant database checks id and status of that restaurant, if status says available, status updates to booked. but in my case if another user book the same restaurant it also showing him booking confirmation even database is updated. – Najam Us Saqib Sep 05 '17 at 16:23
  • You should try to `console.log` the `doc` and see what you send back to the user `(res)`. It should work. My guess is when you do `if (res == true)`, res is casted to boolean and tho to `true`, but actually `res.send(false)` is sent – Orelsanpls Sep 06 '17 at 07:07
  • console.log(doc); OutPut: `{ lastErrorObject: { updatedExisting: false, n: 0 }, value: null, ok: 1 } ` – Najam Us Saqib Sep 06 '17 at 14:06
  • So exactly, you have to interpret the data of doc. Because it will always evaluate to `true` – Orelsanpls Sep 06 '17 at 14:21
  • how can i interpret `doc` i am novice in JS – Najam Us Saqib Sep 06 '17 at 14:33
0

Note:

  1. Since you are checking availability status="yes", Better hard code, instead of getting it from request query/data.
  2. Change the response according to your requirement res.send(true)/ res.send(false).

Following code will work

app.post('/booking', function (req, res) {

  var collection = req.db.get('restaurant');

  collection.findOneAndUpdate({
        status: "yes",
       _id: req.body.id
     }, {
        $set: {
        status: "booked"
     }
  }, function (err, result) {
   
        //Error handling
        if (err) {
           return res.status(500).send('Something broke!');
        }

       //Send response based on the required
        if (result.hasOwnProperty("value") && 
          result.value !== null) {
             res.send(true);
        } else {
             res.send(false);
        }
   });
});
Community
  • 1
  • 1
karthick
  • 5,998
  • 12
  • 52
  • 90