【发布时间】:2020-10-14 17:34:52
【问题描述】:
我目前正在使用 React Native 和 iTunes 的搜索 API 编写我的第一个应用程序。
我放了一个 TextInput 和一个 Button,它返回与文本对应的所有专辑。点击相册后,它会获取其唯一 ID 并搜索此相册的详细信息。
export function getAlbumDetailFromApi(id)
{
return fetch('https://itunes.apple.com/lookup?id='+id)
.then((response) => response.json())
.catch((error) => console.log(error));
}
这是课程:
class AlbumDetail extends React.Component {
constructor(props) {
super(props)
this.state = {
album: undefined,
isLoading: true
}
}
componentDidMount() {
console.log(this.props.navigation.state.params.idAlbum)
getAlbumDetailFromApi(this.props.navigation.state.params.idAlbum).then(data => {
this.setState({
album: data,
isLoading: false
})
})
}
_displayLoading() {
if (this.state.isLoading) {
return (
<View style={styles.loading_container}>
<ActivityIndicator size='large' />
</View>
)
}
}
_displayFilm() {
const { album } = this.state
console.log(album.results.artistId)
console.log()
if (album != undefined) {
return (
<ScrollView style={styles.scrollview_container}>
<Text style={styles.title_text}>name of the album should go here</Text>
</ScrollView>
)
}
}
render() {
return (
<View style={styles.main_container}>
{this._displayLoading()}
{this._displayFilm()}
</View>
)
}
}
如果我的专辑 ID 是“1474669063”,该函数将返回:
但这是我的问题。也许它超级简单,但我已经尝试了我找到的所有东西,但我无法访问数据......
有人可以帮帮我吗?
【问题讨论】:
-
错误是什么?
-
@GauravRoy 它说'未定义不是一个对象(评估'album.results')
-
请不要在标题中添加“SOLVED”。当一个答案被接受时,它会自动将问题标记为已解决,因此不需要在标题中使用它。
标签: javascript arrays json react-native jsx