我在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/