Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在React Native中获取对象中的特定值?_Javascript_Arrays_Json_React Native_Jsx - Fatal编程技术网

Javascript 如何在React Native中获取对象中的特定值?

Javascript 如何在React Native中获取对象中的特定值?,javascript,arrays,json,react-native,jsx,Javascript,Arrays,Json,React Native,Jsx,我目前正在使用React Native和iTunes的搜索API编写我的第一个应用程序 我放了一个文本输入和一个按钮,返回与文本对应的所有相册。单击相册后,它将获取其唯一ID并搜索此相册的详细信息 export function getAlbumDetailFromApi(id) { return fetch('https://itunes.apple.com/lookup?id='+id) .then((response) => response.json()) .catch

我目前正在使用React Native和iTunes的搜索API编写我的第一个应用程序

我放了一个文本输入和一个按钮,返回与文本对应的所有相册。单击相册后,它将获取其唯一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>
    )
  }
}

类AlbumDetail扩展了React.Component{
建造师(道具){
超级(道具)
此.state={
专辑:未定义,
孤岛加载:正确
}
}
componentDidMount(){
log(this.props.navigation.state.params.idAlbum)
getAlbumDetailFromApi(this.props.navigation.state.params.idAlbum)。然后(data=>{
这是我的国家({
专辑:数据,
孤岛加载:false
})
})
}
_显示加载(){
if(此.state.isLoading){
返回(

但这是我的问题。也许它非常简单,但我已经尝试了我发现的一切,但我无法访问数据


有人能帮我吗?

问题出在函数
getAlbumDetailFromApi

它不返回响应。它返回的是
promise

考虑到您的代码,您应该使用
async/wait
并仅返回响应。因此,您的
setState
具有数据部分

export async function getAlbumDetailFromApi(id) {
  try {
    const response = await fetch('https://itunes.apple.com/lookup?id=' + id);
    return response.json();
  } catch (error) {
    console.log(error);
  }
}

抓取:

  getAlbumDetailFromApi(id) {
    return fetch('https://itunes.apple.com/lookup?id='+id)
      .then(response => response.json())
      .then(data => {
        // set the state here, we access the first object of the results array with data.results[0}
        this.setState({album: data.results[0]})
        })
    .catch((error) => console.log(error));
  }
componentDidMount() {
    this.getAlbumDetailFromApi(1474669063);
  }
  renderAlbum() {
    // if we have an album display it 
    if (this.state.album) {
      return (
        <View>
        <Text> Arist: {this.state.album.artistName}</Text>
        <Text> Release Date: {this.state.album.releaseDate}</Text>
        </View>
      );
    }
    
  }
  render() {
    console.log('state', this.state);
    return (
    <View style={styles.container}>
        {this.renderAlbum()}
    </View>
  );
  }
渲染方法:

  getAlbumDetailFromApi(id) {
    return fetch('https://itunes.apple.com/lookup?id='+id)
      .then(response => response.json())
      .then(data => {
        // set the state here, we access the first object of the results array with data.results[0}
        this.setState({album: data.results[0]})
        })
    .catch((error) => console.log(error));
  }
componentDidMount() {
    this.getAlbumDetailFromApi(1474669063);
  }
  renderAlbum() {
    // if we have an album display it 
    if (this.state.album) {
      return (
        <View>
        <Text> Arist: {this.state.album.artistName}</Text>
        <Text> Release Date: {this.state.album.releaseDate}</Text>
        </View>
      );
    }
    
  }
  render() {
    console.log('state', this.state);
    return (
    <View style={styles.container}>
        {this.renderAlbum()}
    </View>
  );
  }
componentDidMount(){
这是从API(1474669063)获取AlbumDetails;
}
renderAlbum(){
//如果我们有相册,就把它显示出来
if(this.state.album){
返回(
阿里斯特:{this.state.album.artistName}
发布日期:{this.state.album.releaseDate}
);
}
}
render(){
console.log('state',this.state);
返回(
{this.renderAlbum()}
);
}
演示


错误是什么?@GauravRoy它说“未定义”不是对象(正在评估“album.results”)。请不要在标题中添加“已解决”。当答案被接受时,它会自动将问题标记为已解决,因此标题中不必包含该问题。感谢您的帮助!但现在,您知道如何访问“结果”中的数据了吗?我总是得到“undefined is not a object”谢谢:)@nicvlas不客气。请接受我的答案(通过单击我答案旁边的复选图标)并向上投票(通过按我答案旁边的“^”。谢谢