0
votes

I set a documents key in a collection to the userId, and now I would like to get that one document in a snapshot for realtime results:

Ref = firestore().collection('Ref');
Ref.where("[.key]","==",this.id).onSnapshot(snap => {
      if (!snap.empty) {
        snap.docChanges().forEach(change => {
          if (change.type === "modified") {
            // update model here for charts
            this.user = change.doc.data()
          }
          if (change.type === "added") {
            // create a new chart
            this.user = change.doc.data()

          }

        })
      }
    })

I tried key .key [.key] nothing is working. I would like to watch this one document for changes while its user is logged in.

1

1 Answers

1
votes

If you want to get an element based on it's id, there is no need to use a query, simply get only a reference to that document id. So please change the following line of code:

 Ref.where("[.key]","==",this.id).onSnapshot(/* ... */)

to

 Ref.doc(key).onSnapshot(/* ... */)

Edit:

var doc = db.collection('Ref').doc(key);
var observer = doc.onSnapshot(docSnapshot => {
  console.log(`Received doc snapshot: ${docSnapshot}`);
  // Get data from the docSnapshot object
}, err => {
  console.log(`Encountered error: ${err}`);
});