Я пытаюсь включить нодмейлер в облачную функцию Firebase и вызвать ее с помощью Axios в приложении React.Функция работает с использованием:
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 - заголовок.Я не могу сказать, правильно ли я использую заголовок или мне нужен дополнительный шаг из приложения.
Вот моя функция, которая содержит запрос POST axios:
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')
});
});