node.js - 在 firebase 函数中编写响应操作时出现问题

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

    });
});

最佳答案

res.send() 是一个仅表达的函数。因此,如果您没有使用express 创建服务器,它可能无法工作。相反,你可以尝试这样的事情 -

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

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

相关文章:

node.js - Gulp+Browserify+Babel+Sourcemaps : Source map contains duplicate code

javascript - 如何创建 mongoose 的 Aggregate 对象的副本?

ios - 如何使用 FirebaseAuth 错误代码?

javascript - 如何在将图片上传到 Firebase 存储后检索下载 Url,以便将其保存到 Firebase 数据库?

javascript - 从 Nest api 获取数据不再起作用

heroku - 在 Heroku 上更新 SSL 证书

javascript - Node.js 不能创建 Blob?

node.js - xcode-选择 : error: tool 'xcodebuild' requires Xcode

ssl - 当我已经拥有根域的 SSL 证书时,如何为子域添加在 dnsimple 上购买的 SSL 证书?