【问题标题】:Accessing resolved promise object properties访问已解析的 Promise 对象属性
【发布时间】:2018-02-25 18:29:30
【问题描述】:

我在访问已解析的 Promise 对象属性时遇到问题。

使用 fetch,我有一个 .then 这样做:

.then((response) => console.log(response.json()))

我正在尝试通过这样做来访问响应对象的user 属性:

.then((response) => console.log(response.json().user))

它正在返回undefined

这样做的正确方法是什么?

【问题讨论】:

    标签: javascript promise fetch-api


    【解决方案1】:

    response.json() 返回另一个 Promise。您需要使用另一个 .then() 回调:

    fetch(...)
      .then(response => response.json())
      .then(data => console.log(data.user))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2020-11-28
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 2021-03-05
      • 2020-12-08
      相关资源
      最近更新 更多