javascript - 我正在尝试使用 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))
解决方案
因为在你的 secondthen
中,你没有返回任何东西,因为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));
推荐阅读
- ios - associatedtype 协议一致性问题
- c# - 立即打开 excel,无需使用 interop.excel 将 excel 保存到本地路径
- php - 如何在不影响 SEO 的情况下将 HTML 包含在另一个 HTML 中?
- shell - 寻找更新项目内部环境变量的最佳实践
- javascript - 如何在 React Native 中“绘制”可触摸的分段环、曲线或弧线以构建现代组件
- amazon-web-services - AWS api 网关与 AWS SQS 集成中的具体格式消息属性是什么?
- mongodb - 安装在 ubuntu 上的 MongoDB 不工作(错误:活动失败)
- javascript - 从当前范围内的另一个文件执行函数
- java - 如何为 CloseableHttpClient httpClient = getHttpClient() 编写 JUnit 测试?
- javascript - 如何将id传递给for循环中的span元素