2017-06-13 4 views
0

私は応答からデータを読み込み、HTMLで設定しようとしています。しかし、私には未定義のエラーが出ています。誰でも私がここで間違っていることを助けることができますか?未定義のプロパティ 'user'を読み取ることができません

後は、コンポーネントの抜粋です:

userProfile: any 
constructor(private _userProfileService: UserProfileService){} 

ngOnInit(){ 
this._userProfileService.getProfileData() 
.subscribe(data => { 
    console.log("Response"); 
    console.log(data); 
      this.userProfile = data; 
      console.log(this.userProfile.user.firstname); 
     }); 

}

HTML:

<h2>{{userProfile.user.firstname}} {{userProfile.user.lastname}}'s Profile <a href="#"><img src="assets/img/pen.png" alt="pen"></a></h2> 
+0

使用:USERPROFILE:任意= {}; –

+0

あなたはなぜ私の答えを最初に受け入れていないのですか? –

+1

@VivekDoshiまず、あなたの答えに感謝し、私の問題を解決しました。私は何ミリ秒で答えを投稿したのかを確認しませんでした。そして、このコミュニティが開発者が問題を解決し、 。 –

答えて

1

安全オペレータこのラインで固定

<div *ngIf='userProfile?.user'> 
<h2>{{userProfile.user.firstname}} {{userProfile.user.lastname}}'s Profile <a href="#"><img src="assets/img/pen.png" alt="pen"></a></h2> 
</div> 
3

問題:

は、あなたがにその利用できる前に、データを表示しようとしているのでビュー。

は、これを試してください

<div *ngIf='userProfile?.user'> 
<h2>{{userProfile.user.firstname}} {{userProfile.user.lastname}}'s Profile <a href="#"><img src="assets/img/pen.png" alt="pen"></a></h2> 
</div> 
関連する問題