cphill cphill - 20 days ago 12
Javascript Question

Nodejs Module Exports TypeError

I am trying to determine what is wrong with my module setup that is throwing the following error:


[TypeError: transactionalEmails.request is not a function]`


It doesn't appear to be how the variable is being exported, but how I am creating the
request
function. Any help on what I'm missing or the mistake that I have made?

transactional-emails.js:

var transactionalEmails = function() {

var helper = require('sendgrid').mail;

request: function request(requester, receiver){

var fromEmail = new helper.Email(requester);
var toEmail = new helper.Email(receiver);

var subject = requester + ' has requested a message.';
var body = new helper.Content('text/plain', 'Hello Email');
var email = new helper.Mail(fromEmail, subject, toEmail, body);

var sg = require('sendgrid')(process.env.EMAIL_API_KEY);

var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: email.toJSON(),
});

return console.log(fromEmail + " Sending a request to " + toEmail);

/*sg.API(request, function(error, response) {
console.log(response.statusCode);
console.log(response.body);
console.log(response.headers);
});*/
}
};
module.exports = transactionalEmails;


Here is how I am calling the function:

var transactionalEmails = require('./transactional-emails');

transactionalEmails.request(req.user.email, req.body.receiverEmail);

Answer

You are exporting a function where you want to export an object.

var helper = require('sendgrid').mail;

var transactionalEmails = {
  request: function request(requester, receiver){
  ...
  }
};

module.exports = transactionalEmails;
Comments