I am trying to understand Q Promises and how to handle two different errors thrown from two different then blocks.
Here is the function I would like to "Promisfy":
router.post('/user', function(req, res) {
var user = new User(req.body);
User.findOne({ email: req.body.email }, function(error, foundUser) {
if(foundUser) {
res.send("Error: User with email " + foundUser.email + " already exists.", 409);
} else {
User.create(user, function(err, createdUser) {
if (err) {
res.send(err, 400);
} else {
res.json({ id: createdUser.id }, 201);
}
});
}
});
});
It takes some User details, and tries to create a new User if one doesn't exist already with the same email. If one does, send a 409. I also handle the normal mongoose error with a 400.
I've tried using mongoose-q to convert it over, and I end up with this:
router.post('/user', function(req, res) {
var user = new User(req.body);
User.findOneQ({email : req.body.email})
.then(function(existingUser) {
if (existingUser) {
res.send("Error: User with email " + existingUser.email + " already exists.", 409);
}
return User.create(user);
})
.then(function(createdUser) {
res.json({ id: createdUser.id }, 201);
})
.fail(function(err) {
res.send(err, 400)
})
});
Is this correct? Is there anyway to push that existing user check into a fail block? I.e. throw an Error, and then catch it and deal with it?
Something like this perhaps:
router.post('/user', function(req, res) {
var user = new User(req.body);
User.findOneQ({email : req.body.email})
.then(function(existingUser) {
if (existingUser) {
throw new Error("Error: User with email " + existingUser.email + " already exists.");
}
return User.create(user);
})
.then(function(createdUser) {
res.json({ id: createdUser.id }, 201);
})
.fail(function(duplicateEmailError) {
res.send(duplicateEmailError.message)
})
.fail(function(mongoError) {
res.send(mongoError, 400)
})
});