angular - TypeError:无法读取离子中未定义的属性“firstName”
问题描述
我创建了一个返回 Promise 的函数
ject
i can access the response's data inside the scoop of the 'then' accessor and read it's value there, but outside the data is not being attributed to the 'User' object so it's returning this error :
>Uncaught (in promise): TypeError: Cannot read property 'firstName' of undefined
解决方案
问题是因为你的 console.log 没有那个功能。
因为函数然后等待响应,当你放出函数时,Angular 会继续这个过程
如果要访问此表单,可以编写如下代码:
async ngOnInit(){
try{
const value = await this.userService.getCurrentUser()
this.user.firstName = value['data']['first_name'];
console.log(this.user.firstName);
}catch(error){
console.log(error)
}
}
推荐阅读
- azure-devops - VSTS - 当发布到以前的环境失败时,我如何阻止发布?
- ruby-on-rails - 单个模型在 Rails 中读取和写入多个表
- matlab - 我正在使用 MATLAB HDL 编码器来转换 matlab 编码器,目前有一些错误
- css - CSS 样式表在一个位置工作,但不在另一个位置
- html - 使用引导程序的 glyhicons 时如何修复“空按钮”Web 可访问性错误
- apache - Apache Geode 调试未知 pdx 类型=2140705
- sphinx - 如何通过 Sphinx 正确搜索数字?
- php - CodeIgniter 添加到购物车不会在名称具有特殊字符的购物车数组中插入数据
- asp.net-mvc - 如何在asp.net mvc的Dropdownlist中获取dataTextField而不是dataValueField?
- python - 如何在 Windows 中获取用户处理百分比