lundi 19 septembre 2016

Res.redirect is not redirecting to a route

The main purpose of this code is to send an email to a recipient. If the sending has an error it will render the error page, if not, it will redirect to a route called /thankyou. The problem here is that my res.redirect does not redirecting to route /thankyou but on the error page. There is no error on the console and I'm pretty sure that I input everything correctly. Any problem on my logic?

var router = function(){
  mailRouter.route('/send')
    .post(function(req, res){
      console.log(req.body);
      var body = req.body;

      var mailgun = new Mailgun({apiKey: api_key, domain: domain});

      var mailOptions = {
        from: body.email,
        to: 'wewe99@yopmail.com',
        subject: 'Oxedio Inquiry',
        text: 'Hi, I am ' + body.name + '. ' + body.message + '. You can contact me @ ' + body.phone
      };

      mailgun.messages().send(mailOptions, function(err, body){
        if(err){
          res.render('error', {error: err});
          console.log(err);
        } else{
          res.redirect('/thankyou');
        };
      });
    });

  mailRouter.route('/thankyou')
    .get(function(req, res){
      res.render('success');
    });

  return mailRouter;
};




Aucun commentaire:

Enregistrer un commentaire