Skip to content

Instantly share code, notes, and snippets.

@bgSosh bgSosh/index.js
Created Mar 2, 2018

Embed
What would you like to do?
Differences error handling behaviour between mongoose versions 4 & 5
//This is intended to illustrate differences error handling behaviour
//between mongoose versions 4 & 5
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mongoosetest', { useMongoClient: true });
var AnimalSchema = new mongoose.Schema({
name: String
});
//This may seem silly, but is contrived to illustrate the problem
AnimalSchema.statics.foo = function (callback) {
var self = this;
var animalId = new mongoose.mongo.ObjectId('5a990a276b84001e1061c5ba'); //Exists in collection
this.findById(animalId, {name : 1}, function(err, animal) {
if(err) {
return callback(new Error('findById returned error: ' + err.toString()));
}
throw new Error('This is a thrown exception');
callback(err);
});
};
var Animal = mongoose.model('Animal', AnimalSchema);
Animal.foo(function(err) {
if(err) {
console.log(err)
} else {
console.log("OK!");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.