I have stored user ID in the comment database
now I want to fetch user data while retrieving comments using the following code
this.firestore.collection("comments", ref => ref.where("enable", "==", true).orderBy("date", "desc").limit(10)).valueChanges({ idField: 'docId' }).subscribe((res) => {
this.comments = res;
res.forEach((element,index) => {
this.firebase.collection("users").doc(element.user).valueChanges().subscribe((res) => {
this.comments[index].userData = res;
})
});
})
HTML Code
<div *ngFor="let item of comments">
<p class="lis">
<img *ngIf="item?.userData?.pic" [src]="item?.userData?.pic" alt="" srcset="" style="width: 30%;float: left; margin: 5%;border: 1px solid white; " />
<span style="font-size: 5vw; font-weight: 600;text-align: justify;">
{{ item.message }}
</span><br>
<span style="font-size: 5vw; font-weight: bold">- {{ item?.userData?.name }} , {{
item?.userData?.city }} ,
{{ item?.userData?.state }}
</span>
<br>
<i class="fa fa-thumbs-up like" aria-hidden="true" style="cursor: pointer;font-size: 5vw;" (click)="like(item?.docId)"></i> {{ item?.like }}
</p>
<div class="row">
<div class="col-3" *ngFor="let img of item?.photos">
<img [src]="img" style="width: 100%;height:auto;cursor: pointer;" (click)="openImg(img)">
<br>
</div>
</div>
</div>
When data get updated HTML page flickers and I don't want that flickering.