nodejs, mongodb-update, async, callback is not consider as a function

760 views Asked by At

I have a problem with the code below. Everything goes fine, until the db.collection.update.

At at console.log(n.6), the callback is not consider as a function anymore. And I don't understand why.

The console display: callback(errorCode404) TypeError: object is not a function

var newData = req.body;
...
async.waterfall([
    function(callback){
        console.log('n3');
        db.getConnection(callback);
    },
    function(db, callback){
        console.log('n4');  
        db.collection('dossiers', callback);
    },
    function(dossiers, callback){
        console.log('n5');
        dossiers.update({'_id': mongodb.ObjectID(id)}, {$set:newData}, callback);
    },
    function(result, callback){
        console.log('n6');
        if(result != 1) {
            console.log('n6.1');
            callback(errorCode404);                         
        } 

        console.log('n6.2');
        callback(null, 'Dossier mise a jour.');

    }
], function(err, result){
    ...
});

Can someone clarify me about this?

1

There are 1 answers

1
JohnnyHK On BEST ANSWER

What's happening is that the update callback has three parameters: error (if any), the count of records that were modified, and a status object. waterfall handles the error parameter, but the last two are passed as parameters to the subsequent waterfall function (n6), with the callback being provided as the third parameter, not the second.

So you need to change that part of your code to be something like:

...
function(dossiers, callback){
    console.log('n5');
    dossiers.update({'_id': mongodb.ObjectID(id)}, {$set:newData}, callback);
},
function(numModified, status, callback){
    console.log('n6');
    ...