繁体   English   中英

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

[英]Issues writing response actions in firebase functions

我在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,但无法弄清楚。 我想念什么?

UPDATE

我将其与以下代码一起使用。 虽然不是我想要的。 我只想返回帐户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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM