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
findOneAndUpdate error handling middleware invoked with wrong arguments #5405
Comments
Confirmed, here's a repro: const assert = require('assert');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
var Schema = mongoose.Schema;
describe('Error Handling Middleware', function() {
before(async function() {
await mongoose.connection.dropDatabase();
});
it('On findOneAndUpdate error and res arguments of handleE11000 should be different', 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(error, res); // This makes the test fail
next();
};
schema.post('findOneAndUpdate', handleE11000);
var Person = mongoose.model('Person', schema);
var person1 = { name: 'p1' };
var person2 = { name: 'p2' };
Person.on('index', function(error) {
assert.ifError(error);
Person.create([person1, person2], function(error) {
// Cause a duplicate key error
Person.findOneAndUpdate({name: 'p2'}, {$set:{name: 'p1'}}, {runValidators:true}, function(error) {
done();
});
});
});
});
}); |
|
So with this fix the 2nd arg will be |
@vkarpov15 |
See Automattic/mongoose#5405 Basically, the doc argument is not available in findOneAndUpdate post hook because MongoDB doesn't give you the document back if findOneAndUpdate() fails.
@vkarpov15 |
Looks like error is passed as the 1st and the 2nd argument of findOneAndUpdate error handling middleware.
The 2nd argument should be the doc instead. Correct me if I'm wrong, because I'm new to this feature and still experimenting.
Here's a repro test inspired by this blog post:
Software version:
Node 6.10.2
Mongodb 3.4.1
Mongoose 4.9.x , 4.10.x and 4.11.0 are all affected by this issue
The text was updated successfully, but these errors were encountered: