app.post method not called when sending sendgrid email in angularjs and node as backend

320 Views Asked by At

I want to send an sendgrid email to a recipient.below is my client code and server code. in the client code, the user object is successfully posted to the url /email. now in the server side the app.post() method isnt called, i can confirm it since the string ''backend was reached' isnt print to the console. Please can someone help me figure this out, thanks.

server/app.js

app.post('/email', function(req, res) {
  logger.debug('backend was reached');
  console.log('backend was reached');
  var payload = {
    to: '[email protected]',
    from: req.body.email.from,
    subject: 'contact',
    html: 'There is a new contact submitted' + '<br>' +
    '<strong> First Name: </strong>' + req.body.firstName + '<br>' +
    '<strong> Last Name: </strong>' + req.body.lastName + '<br>' +
    '<strong> Message: </strong>' + req.body.message + '<br>'
  };
  req.app.get('Sendgrid').send(payload, function (err, json) {
    logger.debug('backend was reached');
    console.log('backend was reached');
    if (err) {
      logger.error('Error sending invite email through sendgrid', {
        error: err.message,
        stack: err.stack,
        source: __filename,
        component: ['invitations', 'sendgrid', 'email']
      });
      res.send(500, {
        message: 'Internal Server Error'
      });
    } else {
      logger.debug('sendgrid succesfully sent invitation notification email');
      console.log('sendgrid sent email');
    }
  });
  res.send(201, {
    message: 'Created'
  });
});

client/contact.js

angular.module('developerPortalApp')
  .controller('ContactCtrl', function ($scope, $http) {
    $scope.message = 'Contact Route';

    $scope.contact = function (user) {
      console.log('success contacting ' + user.email + " " + user.firstName + " " + user.lastName + " "
      + user.message);

      $http.post('/email', user)
        .success(function (data, status) {
          console.log("Sent ok client " + status);
        })
        .error(function (data, status) {
          console.log("Error client " + status);
          console.log(data);
        })

    }

  });
1

There are 1 best solutions below

0
On

so the issue was from me. i was looking at the wrong console to find the log. Everything works find as expected. Thanks