简体   繁体   English

将获取 object 克隆到 React.js 中未定义的 object 的问题

[英]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. 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我搜索的 object,在戳 object 中没有 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在之前添加等待,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 是异步的,所以你需要等待响应,通过 await 或 then

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

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

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