简体   繁体   中英

Problems to clone a fetch object into an undefined object in React.js

i have this trouble, i've defined a poke object and i try to clone to this an object thats give me this link https://pokeapi.co/api/v2/pokemon/1/ but i only get a promise fullfiled with the object i search for, no the object itself in the poke object. Here's the code:

let poke = fetch(url).then(response => response.json()).then(data => JSON.parse(JSON.stringify(data)));
console.log(poke);

Add await before, smth like

async () => {
  let poke = await fetch(url).then(response => response.json()).then(data => JSON.parse(JSON.stringify(data)));
  console.log(poke);
}

fetch is async, so you need to wait response, via await or then

  fetch(url).then(response => response.json()).then(data => JSON.parse(JSON.stringify(data))).then(data => {console.log(data)});

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