[英]React await promise resolve not getting data
I am using async/await
in the following function我在以下 function 中使用async/await
forgotPassword = async ({ variables }) => {
console.log(variables)
const url = `//url constructed`
console.log(url)
try {
const result: any = await fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
mode: 'no-cors'
}
})
console.log('result fetched is -->', result)
return {
data: {
login: result.json()
}
}
} catch (err) {
this.setState({
error:
err?.response?.data?.error_description || err.message || strings.genericError
})
throw err
}
}
So, In the result when I did所以,在我做的结果中
result.json()
i am getting response like:我得到这样的回应:
Promise {<pending>}
__proto__: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Object
Status: "Successful"
Time: "1619013232787"
__proto__: Object
so when I tried doing所以当我尝试做
const status = result?.Status
then it gave me undefined
然后它给了我undefined
的
How to get the response?如何获得响应? even the promise is fulfilled.甚至 promise 也满足了。
You need to await result.json() since it returns a promise.您需要等待 result.json(),因为它返回 promise。 https://developer.mozilla.org/de/docs/Web/API/Body/json https://developer.mozilla.org/de/docs/Web/API/Body/json
try {
const result: any = await fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
mode: 'no-cors'
}
})
const data = await result.json();
return {data};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.