【问题标题】:Issues writing response actions in firebase functions在 Firebase 函数中编写响应操作的问题
【发布时间】:2018-06-24 21:10:53
【问题描述】:

我在 index.js 中有一个函数,我正在尝试从 API 的响应中解析帐户 ID。原始响应如下:

{
    "data": {
        "user": null,
        "account": {
            "id": 865,
            "email": "mitch@gmail.com",
            "plan_identifier": "dnsimple-business",
            "created_at": "2018-06-24T00:55:29Z",
            "updated_at": "2018-06-24T00:56:49Z"
        }
    }
}

我的代码如下:

exports.dnsCheckAuthorization = functions.https.onRequest((req, res) => {
    cors(req, res, () => {
        dnsimple.identity.whoami().then((response) => {
            return res.status(200).send(response.data.account.id);
        }).catch(error => (
            res.status(500).send(error)
        ))

    });
});

最后,我在 PostMan 中收到的错误如下:

Error: could not handle the request

Firebase 日志中的错误如下:

Function execution took 519 ms, finished with status: 'response error'

我已经尝试了所有我能想到的方法来获取此函数返回的 ID,但就是无法弄清楚。我错过了什么?

更新

我让它与以下代码一起工作。虽然不是我想要的。我只想返回帐户 ID。

exports.dnsCheckAuthorization = functions.https.onRequest((req, res) => {
    cors(req, res, () => {
        dnsimple.identity.whoami().then((response) => {
            var accountID = response.data.account.id;
            console.log('account id is', accountID);
            return res.status(200).json({id: accountID});
        }).catch(error => (
            res.status(500).send(error)
        ))

    });
});

【问题讨论】:

  • 似乎您可能希望对函数中的各种值进行一些日志记录,以确保它们是您认为的那样。使用console.log()
  • 检查我的更新
  • 什么是dnsimple.identity.whoami()

标签: node.js firebase dnsimple


【解决方案1】:

res.send() 是一个 express 函数。因此,如果您没有使用 express 创建服务器,它可能无法正常工作。相反,您可以尝试这样的方式 -

res.status(200).end(response.data.account.id)

【讨论】:

    猜你喜欢
    • 2019-07-31
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 2016-11-26
    相关资源
    最近更新 更多