-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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 promise uncaught exception #3355
Comments
For backwards compat until #2917, mongoose returns mpromise promises, which do not have a The reason why you're not getting any messages if you don't use an ES6-compatible promises lib like Bluebird is that the var mongoose = require('mongoose'),
Promise = require('bluebird');
//mongoose.Promise = Promise;
mongoose.set('debug', true);
UserSchema = new mongoose.Schema({
_id: String
});
User = mongoose.model('User', UserSchema);
mongoose.connect('mongodb://localhost/gh3355', function() {
User.remove(function() {
next();
});
});
function next() {
var user = new User({ _id: 'a' });
user.save().then(function(res) {
var user2 = new User({ _id: 'a' });
user2.save().catch(function(err) {
console.log("Caught error - Promise");
});
console.log(1);
var user3 = new User({ _id: 'a' });
user3.save(function(err) {
if(err) console.log("Caught error - Callback");
});
}).then(null, function(error) { console.log(error.stack); });
} You should see the error:
|
@vkarpov15 mongoose.Promise = global.Promise was exactly what I was looking for. Thank you so much for the recommendation. |
I finally got around to promisifying the application I'm working on, and I stumbled upon the following bug when saving a duplicate record with a callback instead of a promise. I added sample code below reproducing the error: when attempting a duplicate insert with a promise and a catch, nothing special happens, but when doing it with a callback it outputs an uncaught exception.
Output:
If you remove the line
mongoose.Promise = Promise;
then the callback stops outputting the message.The text was updated successfully, but these errors were encountered: