我正在尝试将nodemailer包含在Firebase Cloud Function中,并在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')
});
});
答案 0 :(得分:0)
您需要返回承诺以避免超时...
embedded-map
那应该足够了
答案 1 :(得分:0)
弄清楚了。该功能需要具有cors(我尚未完全了解),但这是实现
exports.sendPulse = functions.https.onRequest((req, res) => {
cors(req, res, () => {
//body of function
})
});
别忘了在文件顶部和package.json中要求使用cors