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

    });
});

最佳答案

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

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

关于node.js - 在Firebase函数中编写响应 Action 的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51013996/

10-16 14:43