0
votes

Im making a firebase application, inside my application, i have a listener for commentaries in a post, this is a subcollection of the document post

 firebase.firestore().collection('posts').doc(post.id).collection('commentaries').orderBy('date', 'desc').limit(12)
        .onSnapshot(function (querySnapshot) {
          let commentaries = []
          console.log(querySnapshot.docChanges())
          querySnapshot.forEach(function (doc) {
            commentaries.push({
              id: doc.id,
              creatorId: doc.data().creatorId,
              commentarie: doc.data().comment,
              creatorPhoto: doc.data().creatorPhoto,
              creatorName: doc.data().creatorName,
              date: doc.data().date
            })
          })
        })

The problem is:

My onSnapshot is being called from OTHER USER! If other users comments on OTHER POSTS (not the post.id), onSnapshot is being called (as it would detecting changes in the root collection 'posts' and not the subcollection).

Is something wrong? My user is getting updating from all changes in collection 'posts'!!!

1

1 Answers

0
votes

Your snapshot listener is being invoked for all changes to the collection, within the parameters of the query. Listeners don't care who makes a change to the collection. The only thing that matters is the result of the query, and those results are going to include all documents that match the constraints of the query.

If it matter to who you who makes the change, you will have to include that in the changed data, and change the query to only yield results that match the data.