在数据请求完成通过 ionViewDidLoad 展示页面的时候
![[Angular] ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'name' of undefined [Angular] ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'name' of undefined](http://code.bmoook.com/wp-content/uploads/2023/03/20230309151256-6409f77800fd8.png)
![[Angular] ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'name' of undefined [Angular] ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'name' of undefined](http://code.bmoook.com/wp-content/uploads/2023/03/20230309151258-6409f77a070ee.png)
报错误 : ERROR Error: Uncaught (in promise): TypeError: Cannot read property ‘name’ of undefined
![[Angular] ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'name' of undefined [Angular] ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'name' of undefined](http://code.bmoook.com/wp-content/uploads/2023/03/20230309151300-6409f77c2556c.png)
因为在我们加载对象的时候,用的是异步模式,即使promise(表示异步)立刻被处理返回,但是浏览器在开始加载对象的时候,这个对象还是没有定义,所以也就读不到属性。
解决办法:
<ion-content> <ion-list no-lines> <ion-item> 名称 <span class="fr old-color">{{data?.name}}</span> </ion-item> </ion-list> </ion-content>
![[Angular] ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'name' of undefined [Angular] ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'name' of undefined](http://code.bmoook.com/wp-content/uploads/2023/03/20230309151302-6409f77e7a32a.png)
data?.name 和 data.name 的区别是什么? 加了一个 ? 有什么作用。
就是对象为空时候不报错,本来这个报错是提示没对象,然后加了个? 就是让他跳过这步检测。
还有另外两种情况会导致出现这种报错 :
![[Angular] ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'name' of undefined [Angular] ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'name' of undefined](http://code.bmoook.com/wp-content/uploads/2023/03/20230309151305-6409f78112e1a.png)
原文地址https://blog.csdn.net/weixin_39461487/article/details/79171766
https://blog.csdn.net/Francis123580/article/details/72876541