【发布时间】:2019-06-04 22:01:22
【问题描述】:
我正在尝试将 nodemailer 包含在 Firebase 云函数中,并在 React 应用程序中使用 Axios 调用它。该功能正在使用:
curl -X POST -H "Content-Type:application/json" https://us-central1-journeylife-dev.cloudfunctions.net/sendPulse -d '{"pulseUrl":"journeylife.typeform.com","pulseTitle":"test pulse"}'
当我尝试使用 POST 请求从我的应用调用函数时,它总是超时。
我在 cURL 请求和我的 axios 请求之间看到的唯一区别是标头。我无法判断我是否错误地使用了标头,或者我是否需要从应用程序调用其他步骤。
这是我的包含 axios POST 请求的函数:
sendPulse() {
axios.post('https://us-central1-journeylife-dev.cloudfunctions.net/sendPulse', {
pulseUrl: this.state.pulseUrl,
pulseTitle: this.state.pulseTitle,
}, {
headers: {
'Content-Type': 'application/json'
}
}).then(function () {
document.getElementById('sentPulse').style.display = 'block';
})
}
这是功能(不包括要求和运输工具):
exports.sendPulse = functions.https.onRequest((req, res) => {
if (req.body.pulseUrl == null || req.body.pulseTitle == null) {
return null;
}
// setup e-mail data, even with unicode symbols
const mailOptions = {
from: '"JourneyLIFE " <jonathan@gojourneylife.com>', // sender address (who sends)
to: 'jonathan@kairosgarage.com', // list of receivers (who receives)
subject: req.body.pulseTitle, // Subject line
text: 'Hello world', // plaintext body
html: '<b>Hello world </b><br> This is the first email sent with Nodemailer in Node.js', // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: ' + info.response);
res.status(200).json('Pulse Sent')
});
});
【问题讨论】:
-
嘿!云函数需要返回一个承诺,它看起来不是你的,我要检查的第一个是在 transporter.sendMail 上添加一个返回语句
-
@andresmijares 在控制台日志中添加了返回函数,但我没有得到任何不同的结果
-
检查我的答案
标签: reactjs firebase axios nodemailer