简体   繁体   English

Javascript 异步 promise:无法获取我的数据

[英]Javascript Async promise: cannot get my data

I'm trying to apply a function to map each element that my promise return after a fetch request.我正在尝试将 function 应用于 map 我的 promise 在获取请求后返回的每个元素。 Somehow I can log the result to the console but I cannot use it further more.不知何故,我可以将结果记录到控制台,但我不能进一步使用它。 Here is my code:这是我的代码:

async function init() {
  const res = await fetch(`https://jsonplaceholder.typicode.com/users`);
  const data = await res.json();
  console.log(data);
  function getUsersNames (data){
      data.map((user, index)=>{
          console.log(user[index].name);
      })
  }
}


init();

and this is a screenshot of my output in the console:这是我在控制台中的 output 的屏幕截图: 在此处输入图像描述

Any help would be great!任何帮助都会很棒! Thanks谢谢

The error is at line 7, you are calling user[index].name but it should be user.name .错误在第 7 行,您正在调用user[index].name但它应该是user.name In the map function you are already looping through your array.在 map function 中,您已经在遍历您的阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM