首页 > 解决方案 > How to fix error when importing a profile picture from a server, but to the photo does not always come through?

问题描述

I am trying to import a profile pic to my app, and it is a real hit or a miss as to whether or not it renders. My console log's that the photo is getting sent but it won't always show up. When it doesn't show, I get hit with this error:

TypeError: undefined is not an object (evaluating 'response.photo_data.raw_data')]

Where: 'response.photo_data.raw_data' is server the function that carries the image data.

Below is the code I use to import the photo, as well as the information associated with the profile (which also comes from the server.) Any suggestions are helpful. I am running in Expo Managed Workflow.

Function that pulls data from server to the app

useEffect(() =>{
           const displayPhoto = async () => {
                let response = await
                        client_instance.download_profile_photo()
            getImageData(response.photo_data.raw_data)
        }

displayPhoto()

}

How photo is displayed in the app:

<View>                              
    {imageData && <Avatar.Image source={{uri:`data:image/jpg;base64,${imageData}`}}/>}
</View>

标签: javascriptamazon-web-servicesimagereact-nativeexpo

解决方案


如果photo_data不是响应对象的属性,就会undefined在你试图访问它的时候出现,所以寻找raw_data属性onundefined会导致这个错误。如果不是 photo_data 对象的属性,则在您访问它时raw_data也会如此。undefined因此,您需要查看从服务器获得的响应,以了解为什么有时服务器会以预期的属性响应而有时却没有。

简而言之,与其说是客户端代码的问题,不如说是您正在调用的服务的响应问题。所以使用可以查看原始服务响应的调试工具。也许服务在某些太大的图片上超时?


推荐阅读