Mongodb return old collection

后端 未结 3 2005
甜味超标
甜味超标 2021-01-24 13:05
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 = [];



m         


        
3条回答
  •  清歌不尽
    2021-01-24 13:28

    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);
        })
    
    }
    

提交回复
热议问题