eee
eee

Reputation: 53

Mongoose Model.remove callback isn't giving me count of items deleted?

So basically I have a model that when I call remove on it, the return is an object that I can't access.

Right now when I call remove I want to access the number of items removed and I am unable to.

This is what is returned:

{
'ok': 1
'n': 0
}

this is not what I want. I figured out that n is the count, however when trying to access it via count.n I can't.

Here's my remove code

Shift.remove({
        _id: req.body.id
    }, function(err, count){
        if(err){
            console.log(err);
            res.json({success: false, message:'problem with id'});
        }else if(!err && count === 0){
            res.json({success: false, message:count});
        }else if(!err && count === 1){
            res.json({success: true, message:count});
        }
    });

I've tried

Shift.remove({
        _id: req.body.id
    }, function(err, count){
        if(err){
            console.log(err);
            res.json({success: false, message:'problem with id'});
        }else if(!err && count.n === 0){
            res.json({success: false, message:count});
        }else if(!err && count.n === 1){
            res.json({success: true, message:count});
        }
    });

and I've tried

Shift.remove({
        _id: req.body.id
    }, function(err, count){
        if(err){
            console.log(err);
            res.json({success: false, message:'problem with id'});
        }else if(!err){
            res.json({success: true, message:count.n});
        }
    });

Even in this last example I can't see the count!! What is happening here?

Upvotes: 2

Views: 862

Answers (1)

eee
eee

Reputation: 53

Ok I figured it out. Hopefully this will help someone out there. To access the count I needed to use

count.result.n

not

count.n

Thanks anyways!

Upvotes: 3

Related Questions