Nodejs模块导出TypeError

时间:2016-11-13 19:43:33

标签: javascript node.js module

我正在尝试确定我的模块设置有什么问题,这会导致以下错误:

  

[TypeError:transactionalEmails.request不是函数]`

似乎不是如何导出变量,而是我如何创建request函数。对我所缺少的东西或我所犯的错误有任何帮助吗?

事务-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;

以下是我调用函数的方法:

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

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

1 个答案:

答案 0 :(得分:1)

您正在导出要导出对象的函数。

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

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

module.exports = transactionalEmails;