javascript - 访问已解决的 promise 对象属性

标签 javascript promise fetch-api

我在访问已解决的 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/

相关文章:

javascript - 在 anchor 上显示/ overflow hidden div

javascript - 选项未显示在 react 选择上

javascript - 使用 KendoUI 折线图,如何在类别上附加多个折线标签?

node.js - 在 Node.js 中使用 async/await 处理 promise 拒绝的正确方法是什么?

javascript - 如何使用 Fetch API 将照片上传到 AWS S3?

firefox - 为什么此 CORS 请求仅在 Firefox 中失败?

javascript - 如何设置新的 firebase 插入的 id

javascript - 向 Promise 内的 setTimeout 添加另一个延迟

node.js - 使用 Promise 递归列出目录中的所有文件,而不使用 fs.promises api

javascript - 在 React App 中使用 Axios 将响应数据下载为流