簡體   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我搜索的 object,在戳 object 中沒有 object 本身。 這是代碼:

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

在之前添加等待,smth like

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

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