在Firebase函数中编写响应动作的问题

时间:2018-06-24 21:10:53

标签: node.js firebase dnsimple

我在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)
        ))

    });
});

1 个答案:

答案 0 :(得分:2)

res.send()是仅表达的功能。因此,如果未使用Express创建服务器,则可能无法正常工作。相反,您可以尝试使用类似这样的方法-

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