Sandor Benya
Sandor Benya

Reputation: 11

Azure Node.Js SendGrid has no method 'send'

I'm using Azure Mobile Service with Node.Js, and I need to send email with SendGrid, but I receive an

TypeError: Object # has no method 'send' at exports.post

error message.

Here is my code:

exports.post = function(request, response) {
  var user = '<user>';
  var key = '<key>';
  var sendgrid = require('sendgrid');

  sendgrid.api_user = user;
  sendgrid.api_key = key;

  var email = new sendgrid.Email({
      to: '[email protected]',
      from: '[email protected]',
      subject: 'This is the subject.',
      text: 'This is the body.'
  });

  sendgrid.send(email, function (err, json) {
      if (err) { return console.error(err); }
      console.log(json);
  });

  request.respond(200);}

Why fails Azure SendGrid? Not correct version installed with the Node.Js? How can I access the Node.Js with npm (Node Package Manager) to reinstall the SendGrid package? Or something goes wrong? (The SendGrid already has "send" method, you can check it via GitHub.)

Thanks a lot for any kind of help! Sandor

Upvotes: 0

Views: 276

Answers (1)

Sandor Benya
Sandor Benya

Reputation: 11

Here is the solution. The reference to the sendgrid is not an object. I have to make a SendGrid object, and it has a "send" method.

The correct code is:

exports.post = function(request, response) {

var user = '<user>';
var key = '<key>';
var sendgridReference = require('sendgrid');

var sendgridObject = new sendgridReference.SendGrid();

sendgridObject.api_user = user;
sendgridObject.api_key = key;

var email = new sendgridReference.Email({
    to: '[email protected]',
    from: '[email protected]',
    subject: 'This is the subject.',
    text: 'This is the body.'
});

sendgridObject.send(email, function (err, json) {
    if (err) { return console.error(err); }
    console.log(json);
});

request.respond(200);

};

Upvotes: 1

Related Questions