I'm trying to send an email using SendGrid with Azure Mobile Services. I'm using the sample code here for reference:
http://azure.microsoft.com/en-us/documentation/articles/store-sendgrid-nodejs-how-to-send-email/
exports.post = function(request, response) {
var SendGrid = require('sendgrid');
var toEmail = 'myemail@mydomain.com';
var mail = new SendGrid.Email({
to: toEmail,
from: toEmail,
subject: 'Hello SendGrid',
text: 'This is a sample email message.'
});
var sender = new SendGrid('my_user','my_ key');
};
I'm getting an TypeError creating the sender. The Email object is created as expected. I'm not sure what I'm doing wrong. From looking at the code in sendgrid.js, the exports look correct. Any ideas?
Here is the error:
Error in script '/api/my_api.js'. TypeError: object is not a function
Note: I have added sendgrid using npm
From sendgrid.js
var Sendgrid = function(api_user, api_key, options) {
}
module.exports = Sendgrid;
Per the github docs:
var sendGrid = require('sendgrid')('my_user', 'my_key');
var mail = new sendGrid.Email({
to: toEmail,
from: toEmail,
subject: 'Hello SendGrid',
text: 'This is a sample email message.'
});
sendgrid.Email
is a method of the object returned by instantiating the module. To access sendgrid.Email
, you must call the function returned by requiring SendGrid.
Your code should look as follows:
exports.post = function(request, response) {
var sendgrid = require('sendgrid');
var sender = new sendgrid('my_user','my_ key');
var toEmail = 'myemail@mydomain.com';
var mail = new sender.Email({
to: toEmail,
from: toEmail,
subject: 'Hello SendGrid',
text: 'This is a sample email message.'
});
};
Edit: Corrected method names.
The SendGrid package was not being imported correctly. I was thrown off the error because the Email object was being created correctly.
I rechecked my package.json and updated the project with npm install. Everything started working.