【问题标题】:How to add header to axios POST for nodemailer using Firebase function如何使用 Firebase 功能将标头添加到 nodemailer 的 axios POST
【发布时间】: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


【解决方案1】:

您需要返回一个承诺以避免超时...

  // send mail with defined transport object
    return transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            return res.send(erro.toString());
        }

        console.log('Message sent: ' + info.response);
        return res.status(200).json('Pulse Sent')
    });

应该够了

【讨论】:

  • 不影响任何事情。由于超时而失败,所以我相信在此之前存在问题。此外,仅通过 cURL 请求时也可以使用。我添加了几个控制台日志,看来我什至没有到达 sendMail 功能
  • 接下来要添加的另一件事...基于此,我假设该功能正在中断(req.body.pulseUrl == null || req.body.pulseTitle == null),您可能想检查您是否正确接收到您期望的数据
  • 也认为它可以在浏览器上返回 cors 问题
  • 好的,请求期间似乎没有发送数据。 req.body 是一个空对象。不知道是什么原因造成的,但正在调查它
【解决方案2】:

想通了。该函数需要有cors(我还没有完全理解),但这里是实现

exports.sendPulse = functions.https.onRequest((req, res) => {
    cors(req, res, () => {
         //body of function
    })
});

不要忘记在文件顶部和 package.json 中要求 cors

【讨论】:

    猜你喜欢
    • 2020-08-14
    • 2018-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    相关资源
    最近更新 更多