Deleting item mongodb and node async
You want to remove only from array? If yes, use
for(var i =0, j = item.uploads.length; i < j; i++) { //verify file exits console.log(req.params.file_id); if(item.uploads[i]._id == req.params.file_id){ item.uploads.slice(i, 1); };
And at the end use: user.save(function(err){});If there is anything you want to delete from file system, use:
fs = require('fs');fs.unlink( FILE PATH , function(err) { console.log(err);});
Also you don't really need async version of forEach, cause User.findById is asynchronous itself and whole process goes on background.
This will continue looping through until the end even after it finds the correct items.
There is no "break" for async.forEach. So if you don't want to do unwanted process, use for as I did and append a break point.
There is no async call inside your loops, so you don't need async.forEach(). Using javascript native loops would be just fine:
exports.getApiDelete = function (req, res, next) { User.findById(req.user.id, function(err, user) { if (err) return next(err); console.log("User ID found: "+ user._id); user.profile.keys.forEach(function(el) { if(el.key==req.params.scriptkey){console.log("KEY FOUND");} el.uplaods.forEach(function(item) { console.log(req.params.file_id); if(item._id == req.params.file_id){ // DELETE FUNCTION HERE? } }); }); });};