首页 > 解决方案 > 我正在尝试使用 json 文件,但它不能返回数据

问题描述

我想使用这个 json
但我收到错误 TypeError: Cannot read property 'aFeeds' of undefined 我怎样才能得到这个值?

  return  fetch('https://freemusicarchive.org/featured.json'
    )
        .then(respone => respone.json())
       .then(json => console.log(json))
        .then(json=> json.aFeeds.aTracks)
        .catch(err=>console.log(err))

标签: javascriptjsonreactjsecmascript-6

解决方案


因为在你的 secondthen中,你没有返回任何东西,因为console.log没有返回值。如果要记录它,则必须使用函数体returnjson

return fetch("https://freemusicarchive.org/featured.json")
    .then(respone => respone.json())
    .then(json => {
        console.log(json);
        return json;
    })
    .then(json => json.aFeeds.aTracks)
    .catch(err => console.log(err));

推荐阅读