简体   繁体   中英

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. 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: 在此处输入图像描述

Any help would be great! Thanks

The error is at line 7, you are calling user[index].name but it should be user.name . In the map function you are already looping through your array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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