user13775830
user13775830

Reputation:

How to get document in subcollection shortly after it has been added?

I wonder if there's a method that will provide me with the object I will have just added to this subcollection without the need to rewrite the whole request.

const businessRef = firebase.firestore().collection('movies').doc(id)

businessRef
  .collection('counts')
  .doc(month)
  .set(counts)
  .then(() => console.log('added'))
  .catch(error => console.log('error', error))

Upvotes: 0

Views: 56

Answers (1)

Harif Velarde
Harif Velarde

Reputation: 753

There are different ways to check if one document has changes on your workflow, I suggest you take a look at this document where you can find multiple examples for this situation using the method onSnapshot()

This is an example in Node JS:

let observer = db.collection('cities').where('state', '==', 'CA')
  .onSnapshot(querySnapshot => {
    querySnapshot.docChanges().forEach(change => {
      if (change.type === 'added') {
        console.log('New city: ', change.doc.data());
      }
      if (change.type === 'modified') {
        console.log('Modified city: ', change.doc.data());
      }
      if (change.type === 'removed') {
        console.log('Removed city: ', change.doc.data());
      }
    });
  });

Upvotes: 1

Related Questions