简体   繁体   中英

Firestore data queried with where() does not update in real time

My issue is a very simple one. I am querying data a collection from firestore using the where clause. The data loads only when i hit save in my code but not initially.

My code:

db.collection('projects').where('projectid','in',shareids).onSnapshot(query => {
  let projects = []
  query.forEach(doc => {
    projects.push(doc.data()) 
  })   
  console.log(projects) //shows in console after editing my code (just add a space or anythign that changes the file - but does not display initially)   
})

What is strange is that when querying the collection using doc() instead of where, this issue does not happen and the firestore data loads in real-time.

This code (loads in realtime):

db.collection('users').doc(user.uid).onSnapshot(doc => {
  const userlist = doc.data()
  console.log(userlist)
}) 

Is the issue with firestore or my code?

(Replacing onSnapshot() with.get().then() doesn't work either)

Could there be a problem with the shareids array?

What if you try .where('projectid','==','aSpecificId') ?

Maybe that will show you if the problem is with the .where or with using in and your shareids array.

Since the documentation says:

Use the in operator to combine up to 10 equality ( == ) clauses on the same field with a logical OR . An in query returns documents where the given field matches any of the comparison values.

For example:

citiesRef.where('country', 'in', ['USA', 'Japan']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM