1
router.post('/orders/finish', function(req, res, next) {
var order_id = req.body.order_id;
var user_id  = req.body.user_id;
var table_id = '';

var result = [];



mongo.connect(url, function(err, db) {
    assert.equal(null, err);

    db.collection('tables').update({id: table_id, status: true}, {$set: {status: false}}, function(err, result) {
        assert.equal(null, err);

    });
    var cursorTables = db.collection('tables').find({status: false});
    cursorTables.forEach(function(doc, err) {
        assert.equal(null, err);
        result.push(doc);
    }, function() {
        db.close();
        res.send(JSON.stringify(result));
    });

});

I'm updating table collection and try to get them, but I get old collection without updating. However in the next request its changed.

  • Likely dupe of https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – JohnnyHK Jul 17 '17 at 13:54
  • @JohnnyHK i've used setTimeout, but still not working – Daniyar Abkeyev Jul 17 '17 at 14:18
  • `setTimeout` isn't a fix for this. The linked question about `setTimeout` is a duplicate because it's the same issue of properly dealing with async functions. – JohnnyHK Jul 17 '17 at 14:35

3 Answers3

2

When you make your .find() call, your collection isn't done updating yet.

You can choose to call .find() in the callback of your .update() call, or you could also use promises or async/await depending on your version.

Another solution would be to use findAndModify with the new option:

Optional. When true, returns the modified document rather than the original. The findAndModify() method ignores the new option for remove operations. The default is false.

mjarraya
  • 1,116
  • 1
  • 11
  • 18
0

You should wait for the update to complete before calling find

db.collection('tables').update({id: table_id, status: true}, {$set: {status: false}}, function(err, result) {
        assert.equal(null, err);
        var cursorTables = db.collection('tables').find({status: false});
        cursorTables.forEach(function(doc, err) {
            assert.equal(null, err);
            resultTables.push(doc);
        }, function() {
            db.close();
        });
    });
Nick Shvelidze
  • 1,564
  • 1
  • 14
  • 28
0

I recommend you use Async

router.post('/', function(req, res) {
    var order_id = req.body.order_id;
    var user_id  = req.body.user_id;
    var table_id = '';

    mongo.connect(url, table_id, function(err, db) {
        myFuntion(db, table_id, function(result) {
            res.send(JSON.stringify(result)); // it should be what you need
        })
    })

});

function myFuntion(db, table_id, callback) {
    var result = [];
    async.waterfall([
        function(callback) {

            db.collection('tables').update({id: table_id, status: true}, {$set: {status: false}}, function(err, result) {
                assert.equal(null, err);
                callback(null);
            });

        }, function(callback) {
            db.collection('tables').find({status: false}, function(err, docs) {
                docs.forEach(function(doc) {
                    result.push(doc);
                })
                callback(null, result);
            });
        }
    ], function(err, result) {
        callback(result);
    })

}
Burdy
  • 603
  • 4
  • 11