gpt4 book ai didi

javascript - Nodejs 模块导出 TypeError

转载 作者:行者123 更新时间:2023-12-03 05:39:57 25 4
gpt4 key购买 nike

我正在尝试确定我的模块设置出了什么问题,引发了以下错误:

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

这似乎不是变量的导出方式,而是我如何创建 request 函数。对我遗漏的内容或我犯的错误有什么帮助吗?

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;

这是我调用该函数的方式:

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

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

最佳答案

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

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

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

module.exports = transactionalEmails;

关于javascript - Nodejs 模块导出 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40578067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com