Search code examples
node.jsfirebasednsimple

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. 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:

Error: could not handle the request

And the error in the Firebase log is the following:

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. What am I missing?

UPDATE

I got this to work with the following code. Not quite what I want though. I want to return just the account 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)
        ))

    });
});

Solution

  • res.send() is an express only function. So it may not work if you did not create your server using express. Instead, you could use try something like this -

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