我正在尝试使用json文件,但无法返回数据

时间:2019-04-14 05:18:06

标签: javascript json reactjs ecmascript-6

我要使用这个json
但我收到错误TypeError:无法读取未定义的属性'aFeeds' 我如何获得价值?

  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))

1 个答案:

答案 0 :(得分:2)

因为在第二个then中,您不会返回任何内容,因为console.log没有返回值。如果要记录它,则必须使用函数体和return json

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));