简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM