firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Uncaught Error in snapshot listener: {"code":"failed-precondition","name":"FirebaseError"}

Open MubashirWaheed opened this issue 3 years ago • 10 comments

[REQUIRED] Describe your environment

  • Operating System version: Ubuntu 20.04.4 LTS
  • Browser version: 104.0 (64-bit) Mozilla Firefox for Ubuntu canonical - 1.0
  • Firebase SDK version: firebase: 9.9.4
  • Firebase Product: firestore

[REQUIRED] Describe the problem

Steps to reproduce:

I am not getting the link to add an index for the compound queries in my console. and instead, I am getting this error in the console [2022-09-18T05:53:50.590Z] @firebase/firestore: Firestore (9.9.4): Uncaught Error in snapshot listener: {"code":"failed-precondition","name":"FirebaseError"} After going through stackoverfollow I manually created the composite indexes in the firebase console under the firestore> indexes.

  1. index Descending, createdBy Descending
  2. index Descending, createdBy Arrays

for both indexes

  • collection ID is posts
  • Query scope collection
  • status enabled

When I remove the orderBy there is no error.

Relevant Code:

following is an array with string values and the index field has number as its value ps: I am using react.

// TODO(you): code here to reproduce the problem
    useEffect(()=>{
        const q = query(
        collection(db, "posts"),
        orderBy("index", "desc"),
        where("createdBy", "in", following)
      );
        const unsubscribe = onSnapshot(q, (querySnapshot) => {
        const data = [];
        querySnapshot.forEach((doc) => {
          data.push(doc.data());
        });
        setPosts(data);
      });
}, [])

MubashirWaheed avatar Sep 18 '22 08:09 MubashirWaheed

Im getting the same error after deleting my yarn.lock and running yarn install which in my case, among other things, it updates @firebase/firestore from 3.4.0 to 3.5.0. After doing that, if i try to login in my app and request data from firebase all my rules fail because the request.auth is sending null all of the sudden, and i get the error: @firebase/firestore: Firestore (9.10.0): Uncaught Error in snapshot listener: {"code":"permission-denied","name":"FirebaseError"}

Screenshot from Firebase Emulator Suit: image

IvoBiaus avatar Sep 19 '22 13:09 IvoBiaus

@IvoBiaus, can you update your emulator and let me know if that helps you? A security update was released in 3.5.0 which changed how the SDK authenticates to the back end. This is likely the source of your issue.

@MubashirWaheed, your issue looks different, but it would be worth updating the emulator if you are seeing this issue when using the emulator and also on Firestore 3.5.0 / Firebase 9.10.0. Just a thought, do you get any additional error info in the network trace of the browser console?

MarkDuckworth avatar Sep 19 '22 16:09 MarkDuckworth

@MarkDuckworth I updated the firebase in my react project to 9.10.0 but I am still get the same error and no I am not getting any other error in network tab. Also a point to note is I am using firebase console instead of emulator

MubashirWaheed avatar Sep 19 '22 22:09 MubashirWaheed

@MubashirWaheed, thanks for checking on that. I was getting the same error and then I configured this index

  • Collection ID = products
  • Fields Indexed = createdBy Ascending, index Descending
  • Query scope = Collection

Putting the fields indexed in a different order did not work for me.

MarkDuckworth avatar Sep 20 '22 03:09 MarkDuckworth

@MarkDuckworth so do you think it's a firebase sdk bug?

MubashirWaheed avatar Sep 20 '22 15:09 MubashirWaheed

Looks like there is an issue when it is not logging a link to create the index. I will confirm with the team.

Did you get your index to work?

MarkDuckworth avatar Sep 20 '22 17:09 MarkDuckworth

A manually created composite index is also not working for me

MubashirWaheed avatar Sep 20 '22 17:09 MubashirWaheed

Execute getDocs(q) instead of onSnapshot(q, ...). That will give you the link you need to create the correct index.

MarkDuckworth avatar Sep 20 '22 17:09 MarkDuckworth

On 9.9.4 and 9.10 I'm not getting the link. Had to rollback to 9.8.4 to get it again. Quite a showstopper this.

chocobuckle avatar Sep 21 '22 15:09 chocobuckle

Just for the record, I didn't try 9.9.0 - 9.9.3, so can't confirm if they give the link or not.

chocobuckle avatar Sep 21 '22 15:09 chocobuckle

Thanks for the continued updates. We're looking to get this fixed.

In the meantime if this is affecting you pass your query to getDocs() instead of onSnapshot() one time to get the link to generate the index.

MarkDuckworth avatar Sep 22 '22 16:09 MarkDuckworth

Sorry for the delayed response but I have the indexes working. As you suggested I used getDocs() to get the link to create the correct index

MubashirWaheed avatar Sep 23 '22 02:09 MubashirWaheed

I'm having exactly the same issue. on React-Native Expo.

I think it has nothing to do with Expo or react. Its mostly based on Firestore indexing !

When I removed the orderBy it started working. If I remove the where() it also works. But together ! They dont work !.

export const commentListener = (postId, setCommentList) => { const collectionRef = collection(db, 'comments'); const postID = postId console.log(postID) const q = query( collectionRef, where('postId', '==', postId), orderBy('creation', 'desc') ); onSnapshot(q, ((snapshot)=> { let comments = snapshot.docs.map((doc) => { const id = doc.id; const data=doc.data(); return { id, ...data} }) setCommentList(comments) })) };

This shows an Error {"code":"failed-precondition","name":"FirebaseError"}

But remove either the 'where( ... )' or the 'orderBy(...)' then there is no error. ! I hope someone solve this, I need to move on with my code and live without indexing by date ..untill I figure it out tomorrow.

ahmadaIanazi avatar Oct 18 '22 08:10 ahmadaIanazi

Execute getDocs(q) instead of onSnapshot(q, ...). That will give you the link you need to create the correct index.

getDocs(q) makes index link properly. Thank you!

DevYuns avatar Oct 21 '22 10:10 DevYuns

Hi @MubashirWaheed. An error handler should be able to help with this problem: https://firebase.google.com/docs/firestore/query-data/listen#handle_listen_errors

Could you please try this code and see if we can get the expected error message with a link to generate the index. Once the composite index is generated, the error should go away.

// TODO(you): code here to reproduce the problem
  useEffect(() => {
    const q = query(
      collection(db, 'posts'),
      orderBy('index', 'desc'),
      where('createdBy', 'in', following)
    );
    const unsubscribe = onSnapshot(
      q, 
      querySnapshot => {
        const data = [];
        querySnapshot.forEach(doc => {
          data.push(doc.data());
        });
        setPosts(data);
      }),
      error => {
        // if index is missing, error message should provide a link to generate required index.
        console.log(error);
      };
  }, []);

Meanwhile, we will be keep looking into default error handling to improve customer experience.

milaGGL avatar Oct 24 '22 21:10 milaGGL

FIY, the bug fix has been merged, and will be included in the next release.

milaGGL avatar Oct 28 '22 16:10 milaGGL

The bug fix was released in 9.14.0 released November 10, 2022: https://firebase.google.com/support/release-notes/js#version_9140_-_november_10_2022

dconeybe avatar Nov 12 '22 22:11 dconeybe