【问题标题】:ReactJS - TypeError: Cannot read property 'raw' of undefinedReactJS - TypeError:无法读取未定义的属性“原始”
【发布时间】:2020-05-15 08:08:35
【问题描述】:

我正在尝试从这样的 api 的一部分获取数据,实际上 我可以在控制台中看到所有数据。

{ 
  "id":"DszAeHV8zfQ",
  "created_at":"2020-01-28T19:41:06-05:00",
  "updated_at":"2020-01-29T05:54:02-05:00",
  "promoted_at":"2020-01-29T05:54:02-05:00",
  "width":3887,
  "height":5595,
  "color":"#1E130A",
  "description":"Yet another sleeping koala. I am not happy with the lighting in this photo, where the koala is in the deep shade of an orange/brown tarpaulin (which is needed to give them shelter from the rain and the sun), and the bright light background, but I can't help being amused by the way they can sleep anywhere in any position.",
  "alt_description":null,
  "urls":{ 
     "raw":"https://images.unsplash.com/photo-1580258387643-65bb85ef9c61?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjExMjkzNX0",
     "full":"https://images.unsplash.com/photo-1580258387643-65bb85ef9c61?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjExMjkzNX0",
     "regular":"https://images.unsplash.com/photo-1580258387643-65bb85ef9c61?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExMjkzNX0",
     "small":"https://images.unsplash.com/photo-1580258387643-65bb85ef9c61?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=400&fit=max&ixid=eyJhcHBfaWQiOjExMjkzNX0",
     "thumb":"https://images.unsplash.com/photo-1580258387643-65bb85ef9c61?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&ixid=eyJhcHBfaWQiOjExMjkzNX0"
  },
  "links":{ 
     "self":"https://api.unsplash.com/photos/DszAeHV8zfQ",
     "html":"https://unsplash.com/photos/DszAeHV8zfQ",
     "download":"https://unsplash.com/photos/DszAeHV8zfQ/download",
     "download_location":"https://api.unsplash.com/photos/DszAeHV8zfQ/download"
  },
  "categories":[ 

  ],
  "likes":2,
  "liked_by_user":false,
  "current_user_collections":[ 

  ],
  "user":{ 
     "id":"TYLyWjPA9BM",
     "updated_at":"2020-01-29T05:54:02-05:00",
     "username":"davidclode",
     "name":"David Clode",
     "first_name":"David",
     "last_name":"Clode",
     "twitter_username":null,
     "portfolio_url":"http://tracts4free.wordpress.com",
     "bio":"I enjoy photography, painting, and nature. I lived in South Africa, the UK, and now Australia. More free paintings, photos available at Tracts4Free.WordPress.com, and Reforestation.me. Also now on YouTube.",
     "location":"Cairns, Queensland, Australia.",
     "links":{ 
        "self":"https://api.unsplash.com/users/davidclode",
        "html":"https://unsplash.com/@davidclode",
        "photos":"https://api.unsplash.com/users/davidclode/photos",
        "likes":"https://api.unsplash.com/users/davidclode/likes",
        "portfolio":"https://api.unsplash.com/users/davidclode/portfolio",
        "following":"https://api.unsplash.com/users/davidclode/following",
        "followers":"https://api.unsplash.com/users/davidclode/followers"
     },
     "profile_image":{ 
        "small":"https://images.unsplash.com/profile-1503350572760-b44aa5280785?ixlib=rb-1.2.1&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=32&w=32",
        "medium":"https://images.unsplash.com/profile-1503350572760-b44aa5280785?ixlib=rb-1.2.1&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=64&w=64",
        "large":"https://images.unsplash.com/profile-1503350572760-b44aa5280785?ixlib=rb-1.2.1&q=80&fm=jpg&crop=faces&cs=tinysrgb&fit=crop&h=128&w=128"
     },
     "instagram_username":null,
     "total_collections":11,
     "total_likes":16469,
     "total_photos":676,
     "accepted_tos":true
  }

}

我可以获得“id”、“created_at”字段。但我无法获得像“urls”、“links”这样的嵌套对象

我尝试使用这样的对象表示法获取该数据:

<img src={this.state.photo.urls.raw} alt="" />

但它给了我一个错误:

TypeError: Cannot read property 'raw' of undefined 

我的 fetch 函数是这样的:

 getPhoto = () => {
    fetch(`https://api.unsplash.com/photos/${this.state.photoId}?client_id=API_KEY`)
        .then(res => res.json())
        .then(data => {
            this.setState({ photo: data });
            console.log(this.state.photo);
        })
        .catch(err => console.log(err));
}

我的状态:

constructor(props) {
    super(props);
    this.state = {
        photo: [],
        photoId: this.props.match.params.photoId,
    }
}

【问题讨论】:

    标签: javascript json reactjs object react-router


    【解决方案1】:

    这是因为在第一次渲染中,this.state.photo 是一个数组

    constructor(props) {
        super(props);
        this.state = {
            photo: [],
            photoId: this.props.match.params.photoId,
        }
    }
    

    并且没有属性url

    要修复它,你应该

    photo 更改为对象

    constructor(props) {
        super(props);
        this.state = {
            photo: {}, // object
            photoId: this.props.match.params.photoId,
        }
    }
    

    在访问之前检查this.state.photo.url是否存在

    <img src={this.state.photo.urls && this.state.photo.urls.raw} alt="" />
    

    当图像尚未加载时,您还可以使用默认 src

    <img src={this.state.photo.urls ? this.state.photo.urls.raw : 'default url'} alt="" />
    

    【讨论】:

    • 当我将它从数组转换为对象时,它也不起作用。但是当我将 更改为 有效。为什么这样有效?
    • 是因为 this.state.photo.urls 没有立即正确加载吗?为什么我必须检查 this.state.photo.urls 是否为真?
    • 是的,因为它没有立即加载。您正在进行异步调用( api call ),并且需要一些时间来获取数据。这里发生的是,在您的组件的第一次渲染时,this.state.photo 是您放在构造函数上的值,并且只有在异步调用完成后,您才设置使组件使用正确数据重新渲染的状态。在第一次渲染时,this.state.photo[],你不能这样做 [].url.raw 因为[].urlundefined,你不能从 undefined 访问属性。
    • 您可以做的一件事是在构造函数中设置this.state = { photo: { url: { raw: 'default-url' } } } 这样您就不需要进行检查,但是最好进行检查,因为如果您的 api 请求返回不同的东西根据您的预期,它会给出同样的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2022-07-22
    • 2019-05-07
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多