Search code examples
javascriptfirebasegoogle-cloud-firestoreangularfire2

Firestore: subscribe to document field


in my Firestore I have users collection, every user have notifications field. How I can subscribe to this field values updates?

I tried to do this:

this.fireStore.collection('users').doc(`${uid}/notifications`)
   .valueChanges()
   .subscribe(data => console.log(data))

But I got an error:

Invalid document reference. Document references must have an even number of segments, but users/IIhwHJDRnWcOFfHRv3ApXlovgEn1/notifications has 3


Solution

  • You can't single out a specific field in a document for updates like this. You must target a document, and any time any field in that document changes, you will get a callback. If you think this is too much data to receive in a callback, you should split the data into multiple documents, and listen to only the documents whose fields you are interested in.