Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoose driver never calls remove callback if no record to delete was found #198

Open
raffaele-clevermind opened this issue Aug 2, 2018 · 0 comments

Comments

@raffaele-clevermind
Copy link

raffaele-clevermind commented Aug 2, 2018

Using the mongoose driver, when calling the method remove with a filter that would delete 0 records the callback is never called.
That's because of a piece of code in the mongoose adapter:

query.exec(function (err, data) {
        if (err) return cb(err);
        if (data) {
            var count = data.length || 0;
            for (var i in data) {
                if (typeof data[i] !== 'undefined') {
                    data[i].remove(function () {
                        if (--count === 0) {
                            cb(null, data);
                        }
                    });
                } else {
                    if (--count === 0) {
                        cb(null, data);
                    }
                }
            }
        } else {
            cb(null, data);
        }
    });

The remove method first executes a query with the required filters in order to get all the records to then delete them one by one and trigger the callback after removing the last one:

data[i].remove(function () {
  if (--count === 0) {
    cb(null, data);
  }
 });

The problem is that when no record is found "data" is an array with length 0, which means that the code will enter the "if (data) " condition but never start the for cycle, which means that the callback won't be triggered

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant