error in retrieving the last document in firebase cloud firestore db

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


error in retrieving the last document in firebase cloud firestore db



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;
})

});



}









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham