Currently, I am using "ionic-angular": "3.7.1" along with Firebase Cloud Firestore. My goal is to retrieve all the documents from the Post collection whenever they are updated, deleted, or added. I have been informed that by calling the onSnapshot Method, I can achieve this. Therefore, I have defined this method in a service.
getPosts(token: string){
const userId = this.authService.getActiveUser().uid;
let posts = [];
return firebase.firestore().collection("Post").onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
posts.push(doc.data());
});
console.log("Posts: ", posts.join(", "));
return posts;
});
}
Although I am able to see all my posts within the array 'Posts' in the console, when I call this method using this.post.getPosts(token)
in my component, I receive the following output in the console:
ƒ () {
asyncObserver.mute();
firestoreClient.unlisten(internalListener);
}
Therefore, I am seeking guidance on how to create a method in a service that can provide the result of onSnapshot and allow me to utilize it in my Component or Ionic Page effectively.
Helpful Documentation:
https://firebase.google.com/docs/reference/js/firebase.firestore.Query?hl=es-419
https://firebase.google.com/docs/firestore/query-data/listen?hl=es-419