当我尝试从wordpress API中提取Image source_URL时,我得到 TypeError:未定义不是对象(正在评估'subitem.media_details.sizes.medium.source_url')

https://imgur.com/a/1KZdNVc

https://imgur.com/a/WJoKKwh

上面所附图像中的错误消息指向以下代码行
{this.state.data && this.state.data.length > 0 && ( this.state.data.map(post => ({post.featured_media > 0 && post._embedded['wp:featuredmedia'].filter(element => element.id == post.featured_media).map((subitem, index) => (source={{ uri: subitem.media_details.sizes.medium.source_url }}
我已经通过wordpress API浏览了JSON数据,并且肯定有一个URL。

 class HomeScreen extends React.Component {

 constructor(){
 super();
  this.state = {
      data: null,
      loaded: true,
      error: null
   }
}
baseURL = 'https://wordpress-URL';

getData = (ev)=>{
this.setState({loaded:false, error: null});
let url = this.baseURL + '/posts?_embed';

let req = new Request(url, {

    method: 'GET'
});

fetch(req)
.then(response=>response.json())
.then(this.showData)
.catch(this.badStuff)
}
showData = (data)=>{
    this.setState({loaded:true, data});
    console.log(data);
}
badStuff = (err) => {
    this.setState({loaded: true, error: err.message});
}
componentDidMount(){
    this.getData();

}

  render() {

return (
  <ScrollView>
    { this.state.error && ( <Text style={styles.err}>{this.state.error}</Text> )}
    {this.state.data && this.state.data.length > 0 && (
      this.state.data.map(post => (
        <View key={post.id} >
          <TouchableOpacity onPress={() => this.props.navigation.navigate('Details', { article: post.content.rendered, title: post.title.rendered} )} >
            <View style={{ flex: 1, flexDirection: 'row', justifyContent: 'space-evenly', paddingTop: 25 }}
              borderBottomWidth={0.8}
              borderBottomColor={'gold'}  >
              <View>
                {post.featured_media > 0 && post._embedded['wp:featuredmedia'].filter(element => element.id == post.featured_media).map((subitem, index) => (
                  <Image
                    source={{ uri: subitem.media_details.sizes.medium.source_url }}
                    style={{ width: width / 3, height: height / 5, borderRadius: 10 }}
                    key={index} />

                ))
                }
                {/* <Image source={require('../assets/Hotelcalifornia.jpg')}
                  style={{ width: 150, height: 150, borderRadius: 10 }} /> */}
              </View>
              <View style={{ height: 170 }} >
                <Text style={{ width: width / 2, height: height / 5 }}
                  fontSize={30} >
                  {post.title.rendered}
                </Text>
              </View>
            </View>
          </TouchableOpacity>
        </View>
      )))}

  </ScrollView>
);
}
}

我希望网址中的图片会显示在
View 内的标签,但我却得到TypeError:undefined不是对象

最佳答案

调试并检查要映射的已过滤数组中的每个元素,以查看每个元素是否存在所有嵌套对象。 subitem.media_details.sizes.medium.source_url 链下方可能存在一个嵌套对象,该对象至少对于其中一个元素不存在。

关于javascript - 如何修复 ' TypeError: undefined is not an object (evaluating ' subitem.media_details.sizes.medium.source_url')',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54333405/

10-09 14:21