我在访问已解决的 promise 对象属性时遇到问题。
使用 fetch,我有一个 .then
这样做:
.then((response) => console.log(response.json()))
我试图通过这样做来访问响应对象的 user
属性:
.then((response) => console.log(response.json().user))
它返回undefined
正确的做法是什么?
最佳答案
response.json()
返回另一个 Promise。您需要使用另一个 .then()
回调:
fetch(...)
.then(response => response.json())
.then(data => console.log(data.user))
关于javascript - 访问已解决的 promise 对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46258620/