Hi,
The "catch" function is undefined on the promise returned by Model.create function.
// Create user
User.create(userData).then(function(user) {
// Log the user (promisify the passport function)
return new Promise(function(resolve, reject) {
req.logIn(user, function(err) {
if (err) {
reject(err);
} else {
resolve(user);
}
});
}).then(user => res.send(user))
.catch(function(err) { // Here : "User.create().then().then().catch is undefined"
var reason = err.message;
if (reason.indexOf('E11000') > -1) {
if (reason.indexOf('username') > -1) {
reason = 'USERNAME_ALREADY_EXISTS';
} else if (reason.indexOf('email') > -1) {
reason = 'EMAIL_ALREADY_EXISTS';
}
}
return next(new Error(reason));
});
Hi @BenjaminBini, by default no mongoose promises have a .catch()
(backwards compatibility reasons). Re #3441 we're working on documenting this properly. However, in mongoose >= 4.1.0 you can substitute your favorite ES6-compatible promises library. For instance, require('mongoose').Promise = global.Promise;
for native promises, require('mongoose').Promise = require('bluebird');
for bluebird, or require('mongoose').Promise = require('q').Promise;
for Q. Any of these should have .catch()
.
This is awesome, thank you for your answer!
@vkarpov15 , how would this be implemented with ES6's import
? (i'm using native promises)
@Fire-Brand
import Promise from 'bluebird';
import mongoose from 'mongoose';
mongoose.Promise = Promise;
Most helpful comment
Hi @BenjaminBini, by default no mongoose promises have a
.catch()
(backwards compatibility reasons). Re #3441 we're working on documenting this properly. However, in mongoose >= 4.1.0 you can substitute your favorite ES6-compatible promises library. For instance,require('mongoose').Promise = global.Promise;
for native promises,require('mongoose').Promise = require('bluebird');
for bluebird, orrequire('mongoose').Promise = require('q').Promise;
for Q. Any of these should have.catch()
.