Thursday 4 May 2017

Node.js Save User

I am trying to refactor my code below. Currently everything works perfectly in regards to saving a user and charging their credit card.

What I am trying to do is wait until the end of the function to save the user. (Shown below) Currently if a user's credit card is declined it will still create the user account even though it redirects to a "buy" failure page. I need it to not save the user until the end of the function.

How would I change my code to do this?

user.save(function(Usererr) {

        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);
              return res.redirect('/buy')
            }

        if(Usererr){
        return res.redirect('/buy')
        } else {

              //I WANT TO SAVE THE USER HERE
              //I WANT TO SAVE THE USER HERE

              console.log('all looks good')
              res.redirect('/results');
        }
        });
    });
    });



via AndrewLeonardi

No comments:

Post a Comment