2017-10-23 2 views
1

イオン2の問題が非常にあります。コード<span>{{ user.id }}</span>を使用している場合、例外は「プロパティIDを読み取れません」というメッセージとともに表示されます。 this.userService.get(this.user_id)メソッドは別のセクションで使用されていますが、これは問題なく動作しますが、このセクションではそれを手助けすることはできますか?JSONオブジェクトが存在しますが、プロパティが選択されている場合イオン2で未定義を返します

これは私のコードです:


          
  
<!-- my profile.html view --> 
 
    <ion-row> 
 
       <ion-col col-4> 
 
       Tipo de socio 
 
       </ion-col> 
 
       <ion-col col-8> 
 
       <span>{{ user.id }}</span><!-- cannot read id property of a null --> 
 
       </ion-col> 
 
      </ion-row> 
 
      <ion-row> 
 
       <ion-col col-4> 
 
       Tipo de socio 
 
       </ion-col> 
 
       <ion-col col-8> 
 
       <span>{{ user|json }}</span><!-- the data is returned --> 
 
       </ion-col> 
 
      </ion-row>
// My profile.ts (where I'm loading the view and calling the user services) 
 

 
    import { User, UserService } from '../../app/models/user'; 
 
    @Component({ 
 
     selector: 'page-profile', 
 
     templateUrl: 'profile.html' 
 
    }) 
 
    export class ProfilePage { 
 
     title = 'My Profile'; 
 
     user: User = null; 
 
     private user_id: number = 0; 
 
     constructor(
 
     public navCtrl: NavController, 
 
     public navParams: NavParams, 
 
     public alertCtrl: AlertController, 
 
     public modalCtrl: ModalController, 
 
     public loadingCtrl: LoadingController, 
 
     public userService: UserService) { 
 
     this.user_id = this.navParams.get('user_id'); // this value is working fine! 
 
     if (this.user_id > 0) { 
 
      this.userService.get(this.user_id).then(user => this.user = user); 
 
     } 
 
     } 
 
     ionViewDidLoad() { } 
 
    }
// My user.ts (where I've my services) 
 

 
    @Injectable() 
 
    export class User { 
 
     id: number; 
 
     name: string; 
 
     email: string; 
 
     ic: number; 
 
     birth_date: string; 
 
     home_phone: number; 
 
     phone: number; 
 
     is_active: boolean; 
 
     is_admin: boolean; 
 
     from_social_account: boolean; 
 
     is_superuser: boolean; 
 
     status: boolean; 
 
     created_at: any; 
 
     updated_at: any; 
 
     groups: any; 
 
     permissions: any; 
 
     addresses: any; 
 
     all_permissions: any; 
 
     auth_token: string; 
 
     is_customer: boolean; 
 
     is_partner: boolean; 
 
     last_login: string; 
 
     profile: any; 
 
     requests: any; 
 
     skills: any; 
 
     social_providers: any; 
 
     wallets: any; 
 
     errors: any = null; 
 
     birth_date_no_formatted: string; 
 
     main_address: any; 
 
    } 
 
    @Injectable() 
 
    export class UserService { 
 
     private headers: Headers = new Headers(Constant.DEFAULT_HEADERS); 
 
     private url: string = `${Constant.API_HOST}${Constant.API_PREFIX}${Constant.API_V1}`; 
 
     constructor(
 
     private http: Http) { } 
 
     get(_id: number): Promise<User> { 
 
     let url = `${this.url}users/${_id}/`; 
 
     return this.http.get(url, { headers: this.headers }) 
 
      .toPromise().then(response => response.json() as User).catch(this.handleError); 
 
     } 
 
     
 
     private handleError(error: any): any { 
 
     console.log(JSON.stringify(error)); 
 
     return {errors: StaticMethods.formatSubmitErrors(error.json()) }; 
 
     } 
 
    }
+0

あなたは '{{user | json}}'からの応答を追加できますか? –

+0

こんにちは、お返事いただきありがとうございます、返信されたjsonはこのリンクにありますhttps://pastebin.com/D9pZDFHsありがとうございます。 –

+0

皆様、ありがとうございました。私は初期値を持つオブジェクトクラスを宣言し、うまく働いています!、ありがとう! –

答えて

1

あなたのコードは実際にはなく、リターン約束データまで、NULL値を表示しようと動作します。

問題を修正するには、このようなsafe-navigation operatorを使用することができますときに、ユーザーnullでない

{{ user?.id }} 

だから、idと他の人は読み取り専用です。

関連する問題