【发布时间】:2018-06-24 21:10:53
【问题描述】:
我在 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)
))
});
});
【问题讨论】:
-
似乎您可能希望对函数中的各种值进行一些日志记录,以确保它们是您认为的那样。使用
console.log()。 -
检查我的更新
-
什么是
dnsimple.identity.whoami()?