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

Error handling middleware invoked with null document on save() #5466

Closed
davebaol opened this issue Jul 14, 2017 · 3 comments
Closed

Error handling middleware invoked with null document on save() #5466

davebaol opened this issue Jul 14, 2017 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@davebaol
Copy link

davebaol commented Jul 14, 2017

Looks like the fix for issue #5405 breaks error handling middleware on save.
With Mongoose 4.11.2 the 2nd argument of the error handling middleware is indeed null, while it is the document - as expected - with earlier Mongoose versions.

Here's a repro:

const assert = require('assert');
const mongoose = require('mongoose');

const GITHUB_ISSUE = `gh-5466`;

mongoose.connect(`mongodb://localhost:27017/${ GITHUB_ISSUE }`);

var Schema = mongoose.Schema;

describe('Error Handling Middleware', function() {
  before(async function() {
    await mongoose.connection.dropDatabase();
  });

  it('On save res should not be null', function(done) {
    var schema = new Schema({
      name: {
        type: String,
        unique: true
      }
    });
    
    var handleE11000 = function(error, res, next) {
      assert.equal(error.name, 'MongoError');
      assert.equal(error.code, 11000);
      assert.equal(typeof next, 'function');
      assert.notStrictEqual(null, res);  // with mongoose 4.11.1 and below res is the document; with 4.11.2+ is null
      next();
    };

    schema.post('save', handleE11000);

    var Person = mongoose.model('Person', schema);

    var person1 = { name: 'David' };
    var person2 = { name: 'David' };

    Person.on('index', function(error) {
      assert.ifError(error);
      Person.create([person1, person2], function(error) {
        done();
      });
    });
  });
});

Software version:
Node 6.10.2
Mongodb 3.4.1
Mongoose 4.11.2 and above are affected; versions 4.11.1 and below work correctly

@davebaol
Copy link
Author

New mongoose 4.11.3 is affected too.

@vkarpov15 vkarpov15 added this to the 4.11.4 milestone Jul 18, 2017
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 18, 2017
@vkarpov15
Copy link
Collaborator

Thanks for pointing this out, will fix ASAP

vkarpov15 added a commit to mongoosejs/kareem that referenced this issue Jul 20, 2017
vkarpov15 added a commit that referenced this issue Jul 20, 2017
@davebaol
Copy link
Author

@vkarpov15
Thanks, 4.11.4-pre works for me now 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants