【发布时间】:2021-08-03 13:38:32
【问题描述】:
我正在为我的项目使用 react Native Expo 和 firebase。
我的应用程序中有一个触发 Google 云函数的按钮,但响应中总是有一个未定义的正文。
这是云函数的调用
const getResponse = async () => {
await fetch("https://*****************************************/test", {
method: 'POST',
body: JSON.stringify({
user_id: userID,
rep_id: request
})
}).then(response => {
//console.log("body is " + response.body.needToBeVoted);
console.log("body is " + JSON.stringify(response.body));
console.log("status is " + JSON.stringify(response.status));
console.log("all response is " + JSON.stringify(response));
console.log("bodyUsed " + JSON.stringify(response.bodyUsed));
if (response.bodyUsed && response.body != null) {
if (isNaN(response.body)) {
setSteps("result");
} else {
setSteps("bills");
}
}
setResponse(response);
})
}
这是我的日志
body is undefined
status is 200
all response is {"type":"default","status":200,"ok":true,"statusText":"","headers":{"map":{"x-cloud-trace-context":"9ea739ca548e85dbaf9f385244bf9def;o=1","content-type":"application/json; charset=utf-8","etag":"W/\"2-l9Fw4VUO7kr8CvBlt4zaMCqXZ0w\"","server":"Google Frontend","function-execution-id":"l16yrcw4wa9g","x-powered-by":"Express","alt-svc":"h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-T051=\":443\"; ma=2592000,h3-Q050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\"","date":"Tue, 03 Aug 2021 11:16:20 GMT","content-length":"2"}},"url":"https://***************************/test","bodyUsed":false,"_bodyInit":{"_data":{"size":2,"offset":0,"blobId":"7B0C2BD0-ADEC-4F4D-A3C0-35FDF80C2987","type":"application/json","name":"test","__collector":{}}},"_bodyBlob":{"_data":{"size":2,"offset":0,"blobId":"7B0C2BD0-ADEC-4F4D-A3C0-35FDF80C2987","type":"application/json","name":"test","__collector":{}}}}
bodyUsed false
当我检查他们的日志时,我知道我的云功能运行良好,但我不确定我必须用什么来回答
res.send(needToBeVoted);
res.json({ data: needToBeVoted });
这两种方法我都试过了,结果是一样的。
如果有人知道我必须做什么才能阅读我的回复正文,请帮助我!
【问题讨论】:
标签: node.js react-native http google-cloud-functions expo