【问题标题】:Firebase Cloud Function: End an HTTP function in a 'catch'Firebase Cloud Function:在“catch”中结束 HTTP 函数
【发布时间】:2019-07-03 09:29:35
【问题描述】:

我正在使用 Firebase Cloud Function 创建一个 HTTP 函数。 此函数的目的是执行 POST 并返回响应。 (我使用 Axios 来进行 POST)

这是我的代码:

exports.doHttpPost = functions.https.onRequest((request, response) => {
    axios.post(url, data, config)
        .then(response => {
            console.log(response);
            response.status(200).send(response);
        })
        .catch(error => {
            console.log(error);
            // --> What should I write here to end the function? <--
        });
});

我的问题是:如果“axios.post”失败,我该如何结束函数? 我用“response.status(200).send(response)”正确地完成了“then”。但我不知道如何完成'catch'。

【问题讨论】:

标签: node.js firebase promise google-cloud-functions axios


【解决方案1】:

Axios 在错误对象中为您提供response 属性。因此,您应该能够以与成功流程相同的方式代理错误响应(未测试):

exports.doHttpPost = functions.https.onRequest((request, response) => {
    axios.post(url, data, config)
        .catch(error => {
            response.status(error.response.status).send(error.response);
        });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多