1
在控制檯上運行下面的代碼,我得到一個對象數組。使用fetch從API獲取數據,如何在promise解決後訪問數據?
fetch('https://api.github.com/users/chriscoyier/repos')
.then(response => response.json())
.then(data => {
// Here's a list of repos!
console.log(data)
});
以後如何訪問數據?例如,如果我想console.log數據[0] .archive_url,承諾解決後?但是,這給出了一個錯誤「未捕獲的ReferenceError:數據未定義」。我如何訪問該對象數組?
的console.log(數據[0] .archive_url)工作正常,我。 https://jsfiddle.net/dyspkd12/ – Adriani6
將'data'分配給全局變量。 – deceze
@ Adriani6我希望在承諾解決後訪問數據。這隻有在我立即將它放入提取請求時纔有效。 – userden