简体   繁体   中英

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);

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM