I have such div tag in my html file:
<div *ngIf="chat.asReceiver.id != user?.id; else otherParty"> and it always return this error
ERROR TypeError: Cannot read property 'id' of undefined I have multiple times tested my variables and both chat.asReceiver.id and user?.id have values, then I put my div under *ngIf like:
<div *ngIf="user"> <div *ngIf="chat.asReceiver.id != user?.id; else otherParty"> //.... </div> </div> Still error exist.
component
public user: User; chats: any[] = []; constructor( ) { this.getChats(); } ionViewWillEnter() { this.authService.user().subscribe( user => { this.user = user; } ); } getChats() { this.privateChatService.getChats().subscribe((res) => { for (const chat of res.data) { this.chats.push(chat); } }); } Any idea?
Update
how my data look like
Update 2
<div *ngIf="chats.length>0"> <ion-item-sliding *ngFor="let chat of chats; index as indexOfelement;"> <div *ngIf="chat.asReceiver.id != user?.id; else otherParty"> <ion-item class="chat-groups"> <ion-avatar slot="start"> <div *ngIf="chat.asReceiver.photo != null; else placeholderImage"> <img (click)="openImageRes(chat)" class="gImage" routerDirection="forward" [src]="chat.asReceiver.photo"> </div> <ng-template #placeholderImage> <img routerDirection="forward" class="gImage" src="../../assets/placeholders/avatar.jpg"> </ng-template> </ion-avatar> <ion-label routerDirection="forward" [routerLink]="['/tabs/', 'chat', chat.id]"> <h2 style="float: left;width: 80%;"slot="start" [innerHTML]="chat.asReceiver.username"></h2> <ion-badge slot="end" color="primary">{{totalBadges}}</ion-badge> </ion-label> </ion-item> </div> </ion-item-sliding> </div> 