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 badges1 Answer
Reset to default 6You 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