javascript - 如何修复 ' TypeError: undefined is not an object (evaluating ' subitem.media_details.sizes.medium.source_url') '

标签 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://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 }}

我已经查看了来 self 的 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>
);
}
}

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

最佳答案

调试并检查您尝试映射的过滤数组中的每个元素,以查看每个元素是否存在所有嵌套对象。 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/

相关文章:

javascript - 使用 javascript fetch() 获取 md 文件时出现问题

css - 淡入后按钮闪烁

android - 有人成功地在 Android 的 React Native 中使用本地镜像吗?

php - WooCommerce 在 php 中更新产品

css - 通过 json 数据 react 原生动态主题

android - Appcenter Android 版本无法安装在设备中

javascript - 为什么使用 cloneNode() 创建多个音频文件会阻止我控制音频源的属性以及如何覆盖它

javascript - 如何在类文件中创建检查 NetworkConnectionStatus 并在 mainactivity 中使用

javascript - CORS 支持不适用于域

javascript - 如何在 WordPress 站点中实现 (Noty) jquery 通知