最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: callback.apply is not a function - Stack Overflow

programmeradmin1浏览0评论

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 remendations are wele.

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 remendations are wele.

Share Improve this question edited Feb 2, 2017 at 7:26 ibrahim mahrir 31.7k5 gold badges49 silver badges77 bronze badges asked Feb 2, 2017 at 7:23 Girum.DGirum.D 1372 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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 pleting :

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

发布评论

评论列表(0)

  1. 暂无评论