【问题标题】:How to fetch data Array in Promise [duplicate]如何在 Promise 中获取数据数组 [重复]
【发布时间】:2017-03-14 05:01:21
【问题描述】:

我在我的 react JS 应用程序上调用 API:

result = fetch('http://localhost:5000/cities')

result.then(function(response) {
 console.log(response.json());
})

然后输出日志:

Promise
  __proto__ : Promise
  [[PromiseStatus]] : "resolved"
  [[PromiseValue]] : Array[5]
     0: Object
     1: Object
     2: Object
     3: Object
     4: Object
      length: 5
     __proto__: Array[0]

如何获取 Array 上的数据?

我需要获取城市名称列表

【问题讨论】:

    标签: javascript reactjs promise


    【解决方案1】:

    response.json() 行返回一个解析为已解析 JSON 的承诺。

    你可以简单地写

    result.then(function(response) {
      return response.json();
    }).then(function (data) {
      console.log(data);
    });
    

    【讨论】:

    • 太棒了...谢谢阿米尔!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2022-11-27
    相关资源
    最近更新 更多