skip to Main Content

Im using Firebase to store some data in my React app. Its working fine on my local emulator with the same settings, but when i publish the app, i get this error:
@firebase/firestore: Firestore (9.12.1): Uncaught Error in snapshot listener: {"code":"failed-precondition","name":"FirebaseError"}

Im using the where() clause, and i red i need to add some sort of index in my firebase rules.

useEffect(() => {
    if (activeList === 'null') {
    } else {
      const userRef = collection(database, activeList.id);
      const sort = query(userRef, where("checked", "==", showDoneTodos), orderBy('title', 'asc'))
      const unsubsctibeAllTodos = onSnapshot(sort, (snapshot) => {
        setAllTodos(snapshot.docs.map(doc => ({
          id: doc.data().id,
          title: doc.data().title,
          desc: doc.data().desc,
          checked: doc.data().checked
        })
        ))
      })

      return () => {
        unsubsctibeAllTodos()
      }
    }
  }, [activeList, showDoneTodos])

Some info:
activeList is an object with and ID. The problem is that this is generated live so i cant preconfigure any collection ID before i publish.

showDoneTodos is a boolean.

Any guidance would be very welcome!

Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    It seems that Firebase does not provide a link to create the necessary index anymore. They only provide the error code:{"code":"failed-precondition","name":"FirebaseError"}

    The solution is the same, go to Firebase -> Indexes and add it there...

    firebase index rules


  2. Firebase logs this error when the required index is missing. It only shows this error when you use the onSnapshot method. When you use the getDocs methode it will show the good old link to create the index in the Firebase console. A solution is to create a similar request with the getDoc methode, create the index and switch back to the onSnapshot methode.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search