Girum.D
Girum.D

Reputation: 137

TypeError: callback.apply is not a function

I'm a bit new to node and mongoose, I'm trying to delete all the documents in a collection. I am using this code:

app.delete('/accounts', function deleteAccount(req, res, next){

    Account.remove({}, {multi:true});
    res.json({
        message: 'Accounts Deleted!'
    });

});

The issue is when I make an API request to this method, it starts processing and doesn't stop, unless I abort it. The code removes all the documents in my collection, but it is doing it with an error. This is the error it throws:

events.js:141

throw er; // Unhandled 'error' event ^

TypeError: callback.apply is not a function

I want my code to work without this error and I don't want my request to hang while it is processing a requst. Any recommendations are welcome.

Upvotes: 5

Views: 4942

Answers (1)

Ebrahim Pasbani
Ebrahim Pasbani

Reputation: 9406

You need to pass a callback for remove method :

Account.remove({}, function(err, result){
  res.json({
        message: 'Accounts Deleted!'
    });
});

And if you don't want to wait for completing :

var cmd = Account.remove({});
cmd.exec();
res.json({
            message: 'Accounts Deleted!'
        });

Actually remove gets two arguments which the second one in optional. If the second one is present it should be a callback.

In removing documents you don't have multi option.

The exception you get is that exactly for mongoose consider {multi: true} as a callback

Upvotes: 6

Related Questions