javascript - 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>
解决方案
如果photo_data
不是响应对象的属性,就会undefined
在你试图访问它的时候出现,所以寻找raw_data
属性onundefined
会导致这个错误。如果不是 photo_data 对象的属性,则在您访问它时raw_data
也会如此。undefined
因此,您需要查看从服务器获得的响应,以了解为什么有时服务器会以预期的属性响应而有时却没有。
简而言之,与其说是客户端代码的问题,不如说是您正在调用的服务的响应问题。所以使用可以查看原始服务响应的调试工具。也许服务在某些太大的图片上超时?
推荐阅读
- android - 我想在我的应用程序中添加一个共享按钮,但我不明白如何实现它,我在我的应用程序中显示来自 firebase 存储的图像
- javascript - 为什么after方法在jQuery中不起作用?
- spring-boot - 如何使用 Mockito 模拟实例化类的方法?
- apache-poi - docx4j 或 Apache-POI:如何从包含图像和英语以外的语言的 docx 文件中获取段落 ID?
- inno-setup - 如何使用 Inno Setup 为 Windows 命令行创建别名?
- java - Spring REST api OAuth2 验证来自外部授权服务器的令牌
- c# - 简单的异常处理
- c# - C# Reflection - GetCustomAttributes for Type 从父类获取属性
- ejs - 用 ejs 中的现有数据填充表单
- scala - 如何保证仅对 SPARK 数据帧的所有记录的 REST API 调用处理一次