【Angular2】Uncaught (in promise): TypeError: Cannot read property 'id'
来源:互联网 发布:通信网络优化值得做吗 编辑:程序博客网 时间:2024/06/05 05:45
背景
需要显示用户信息,用到了插值表达式和双向绑定
开始数据显示正常,但是浏览器一直报错,不知为何
因为可以正常运行,所以就继续做
但是用到后台返回值来切换编辑状态的时候,什么都不显示
所以就得解决了这个问题
问题
ERROR Error: Uncaught (in promise): TypeError: Cannot read property ‘id’ of undefined
TypeError: Cannot read property ‘id’ of undefined
解决
1.使用箭头函数
getStudents():void{ this.studentService.getStudents().then(students=>this.students=students); }
2.在插值表达式中加入问号
{{student?.name}} //not {{student.name}}
说明:
因为在我们加载对象的时候,用的是异步模式,即使promise立刻被处理返回,但是浏览器在开始加载对象的时候,这个对象还是没有定义,所以也就读不到属性
参考:
https://stackoverflow.com/questions/38078741/angularjs-2-exception-error-uncaught-in-promise-typeerror-cannot-set-prope
阅读全文
0 0
- 【Angular2】Uncaught (in promise): TypeError: Cannot read property 'id'
- JavaScript:Uncaught TypeError Cannot read property 'id' of null。
- ionic3+angular4中给DOM添加监听事件后报错:Uncaught (in promise): TypeError: Cannot read property 'addEventListen
- Uncaught TypeError: Cannot read property 'addEventListener'
- $.getJSON报错Uncaught TypeError: Cannot read property 'id' of undefined解决方法
- React开发问题记录--(in promise) TypeError: Cannot read property 'data' of undefined
- Extjs4---Uncaught TypeError: Cannot read property ‘items’ of undefined
- 【StackFlow】Uncaught TypeError: Cannot read property 'pageCount' of undefined
- Uncaught TypeError: Cannot read property of 'name' null
- Uncaught TypeError: Cannot read property 'length' of null
- Uncaught TypeError: Cannot read property 'length' of null
- Ckeditor: Uncaught TypeError: Cannot read property 'keyCode' of undefined 解决方法
- Uncaught TypeError: Cannot read property 'value' of null
- Uncaught TypeError: Cannot read property 'msie' of undefined问题解决方案
- 【ExtJS】Uncaught TypeError: Cannot read property 'length' of null
- openlayers Uncaught TypeError: Cannot read property 'wrapDateLine' of null
- Uncaught TypeError: Cannot read property 'options' of undefined 错误
- Uncaught TypeError: Cannot read property 'length' of undefined
- CSS3学习笔记1
- c++中图的遍历,dfs和bfs的简单实现
- 光源选择的三个重要图
- 金蝶EAS,序时簿界面ListUI数据不允许修改、删除
- mybatis框架中validationQuery选项的作用
- 【Angular2】Uncaught (in promise): TypeError: Cannot read property 'id'
- mysql中的左连接、内连接、右连接和全连接理解
- CSS学习笔记2
- 云计算的三种服务模式:IaaS,PaaS和SaaS
- 【教程】nrf51822实例代码解析及修改实例
- 使用Anaconda如何实现Python多环境配置?
- LTE资源调度(5)-上行调度请求SR
- redis
- 松下PLC通讯口以太网升级