从提取访问响应数据

时间:2020-09-08 13:09:18

标签: javascript api fetch

这是我的提取函数:

getAllCountries = async () => {
    try {
      const response = await fetch("https://restcountries.eu/rest/v2/all");
      const result = response.json();
      console.log(result);
      this.countriesList = result;
    } catch (error) {
      console.log(error);
    }
  };

在控制台中,它记录 enter image description here

为什么在那里登录了两个Promise,以及如何访问PromiseResult。 我尝试了console.log(result[0]),但没有成功

1 个答案:

答案 0 :(得分:6)

json() 返回一个承诺,所以:

public static bool IsIEnumerableOfT(this Type type)
{
    return type.GetInterfaces().Any(x => x.IsGenericType
           && x.GetGenericTypeDefinition() == typeof(IEnumerable<>));
}

侧面说明:该代码已沦为 getAllCountries = async () => { try { const response = await fetch("https://restcountries.eu/rest/v2/all"); const result = await response.json(); // ^^^^^−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− add console.log(result); this.countriesList = result; } catch (error) { console.log(error); } }; API步枪的猎物:fetch仅拒绝针对 network 错误而不是HTTP错误的承诺。您必须自己检查这些内容,例如:

fetch