Created
March 2, 2018 12:16
-
-
Save srlowe/34055ba6ce4285eda59cfdd43a53b044 to your computer and use it in GitHub Desktop.
Differences error handling behaviour between mongoose versions 4 & 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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