Error in retrieving the last document from firebase

#1

I’m trying to get the last document from my collection using orderBy method and limit(1) everything is Ok and works as expected , but the problem is when I added a new document to my collection from somewhere else such as firebase console while opening the same page in my app I got the last 2 documents although I specified limit(1) . When I refresh the page everything goes well . how could I solve this problem ? Thanks in advance .

– Here is some of my code :

first get my chats ordered descending , then get friend’s details from chatUsers collection , finally get the last message document .

chat-provider.ts

getChats() {
return  this.afs.collection(`chatUsers/${this.userId}/chats` , ref => ref.orderBy('updatedAt' , 'desc')).valueChanges()
}

 getlastMessage(friendId : string) {
return this.afs.collection(`chatUsers/${this.userId}/chats/${friendId}/messages` , ref => ref.limit(1).orderBy('createdAt' , 'desc')).valueChanges();
}

auth-provider.ts

getUser(userId) : Observable<ChatUser> {
return this.afs.doc<ChatUser>(`chatUsers/${userId}`).valueChanges();
}

page.ts

ionViewWillEnter(){

// get chats

this.chatService.getChats()
  .pipe(takeUntil(this.ngUnsubscribe))
  .subscribe((friends : Friend[]) => {

    let chats = [];

    friends.forEach(friend =>{

      //get friend Dtails

      this.chatService.authService.getUser(friend.friendId)
        .pipe(takeUntil(this.ngUnsubscribe))
        .subscribe(friendDetails => {

          //get last message

          this.chatService.getlastMessage(friendDetails.uid)
             .pipe(takeUntil(this.ngUnsubscribe))
             .subscribe((lastMessage : any) => {
               let Chat : ChatMsg = {
                 friend : friendDetails,
                 lastMsg : lastMessage
               }

               chats.push(Chat);

             })

        })
    })


     this.zone.run(() => {
        this.chats = chats;
     })

  });
}