I have a set up that let's a user create an account using PassportJS and charges them at the same time using Stripe. This all works.
The issue is this is currently allowing user's to sign up using the same username. the function catches the error in the user.save part. However even though it catches the error it still finishes creating the new user and charging them. I need to return back there. However I have been unable to figure out how to res.redirect or something at that part.
How would I change my code to redirect there if theres an error?
app.post('/quiz', function(req, res) {
var user = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password,
datapoint: req.body.datapoint
})
user.save(function(err) {
console.log('this is the problem' + ' ' + err)
if(err){
// I NEED TO REDIRECT BACK HERE IF THERE IS AN ERROR
// I NEED TO REDIRECT BACK HERE IF THERE IS AN ERROR
// I NEED TO REDIRECT BACK HERE IF THERE IS AN ERROR
// I NEED TO REDIRECT BACK HERE IF THERE IS AN ERROR
// I NEED TO REDIRECT BACK HERE IF THERE IS AN ERROR
}
var token = req.body.stripeToken; // Using Express
var charge = stripe.charges.create({
amount: 749,
currency: "usd",
description: "Example charge",
source: token,
}, function(err, charge) {
if(err) {
console.log(err);
}
console.log('charged')
req.logIn(user, function(err) {
if(err) {
console.log(err);
}
console.log('all looks good')
res.redirect('/jobquiz');
});
});
});
});
via AndrewLeonardi
No comments:
Post a Comment