首页 > 解决方案 > 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

标签: angulartypescriptionic-framework

解决方案


问题是因为你的 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)
  }
}


推荐阅读