当我尝试从中获取值时,为什么我的对象未定义

时间:2019-02-18 20:00:32

标签: javascript angular typescript lambda

因此,我通过GET请求获取数据。然后,将其加载到对象,然后尝试从中获取数据。但是当我console.log()来自对象的某个值时,它说它是未定义的。

 Users:UserService;  

  ngOnInit() {
    this.GetUsers();
  }
  GetUsers(){
    var tokenUser = JSON.parse(localStorage.getItem('currentuser'));
    this.config.GetUsers().subscribe( data => {
      this.Users = data.find(x => x.username == tokenUser.username);
      console.log(this.Users) //<= Works perfectly fine.
      console.log(this.Users.OrganisationName) //<= gives me an undefined
    });
  }

这是我尝试从对象分配值(未定义)的部分。

 RegisterUserData(event:any){
    if(this.registerForm.valid){
      this.config.PostData(this.user).subscribe((data: UserService) => {
        this.user = {
          FirstName:undefined,
          LastName:undefined,
          Rights:"rw",
          OrganisationName: this.Users.OrganisationName,
          username:undefined,
          password:undefined,
          Email:undefined,

        }
      });
    }
  }

0 个答案:

没有答案