FIrebase firestore onSnapshot not firing on change

Viewed 1315

I am following a simple tutorial on Firebase 9. Everything worked fine until I tried to use onSnapshot method to get the snapshot of changes in db every time the cahnge occurs. But onSnapshot is not working. I mean it work only for the initial fetch of data, but does not execute the callback function when the change occurs. I am following a tutorial so my code is identical to the code from the tutorial. My question is whether there is something I need to do in the console to rectify this, some kind of rules or permissions or something.

My code:

const colRef = collection(db, 'books');

const q = query(colRef, where('author', '==', 'patrick rothfuss'));

onSnapshot(q, (snapshot) => {
  let books = []
  snapshot.docs.forEach(doc => {
    books.push({ ...doc.data(), id: doc.id })
  })
  console.log(books)
})
1 Answers
Related