如何修复“类型错误:未定义不是对象(评估“subitem.media_details.sizes.medium.source_url”)”

Uma*_*745 3 javascript wordpress react-native react-navigation

当我尝试从我的 WordPress API 中提取图像 source_URL 时,出现TypeError: Undefined is not an object (evaluating 'subitem.media_details.sizes.medium.source_url') 。

https://i.stack.imgur.com/rhSZc.jpg

https://i.stack.imgur.com/T4PMO.jpg

上面附图中的错误消息指向以下代码行

{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>
);
}
}
Run Code Online (Sandbox Code Playgroud)

我希望 URL 中的图像显示在视图内的标签中,但我得到了 TypeError: undefined is not an object

小智 5

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

  • 你是对的。新帖子已上传,有些没有特色图片。干杯 (2认同)