简体   繁体   English

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

[英]Issues writing response actions in firebase functions

I have a function in index.js and I'm trying to resolve an account id from a response on an API. 我在index.js中有一个函数,我正在尝试通过API响应来解析帐户ID。 The original response is the following: 原始响应如下:

{
    "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"
        }
    }
}

And my code is the following: 我的代码如下:

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

    });
});

Finally, the error I receive in PostMan is the following: 最后,我在PostMan中收到的错误如下:

Error: could not handle the request

And the error in the Firebase log is the following: Firebase日志中的错误如下:

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

I've tried literally everything I can think of to get just the ID returned by this function and just can't figure it out. 我已经尝试了所有我能想到的一切,只是获得了此函数返回的ID,但无法弄清楚。 What am I missing? 我想念什么?

UPDATE UPDATE

I got this to work with the following code. 我将其与以下代码一起使用。 Not quite what I want though. 虽然不是我想要的。 I want to return just the account 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() is an express only function. res.send()是一个仅表达的功能。 So it may not work if you did not create your server using express. 因此,如果未使用Express创建服务器,则可能无法正常工作。 Instead, you could use try something like this - 相反,您可以尝试使用类似这样的方法-

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

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

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