1

I have sorted by timestamp in my app before and its worked fine on my messages and such but when I try sorting it for my groupchats it is giving me an error

Structure

Thats my structure and the query im using is

FirebaseFirestore.instance
              .collection('chats')
              .where(
                'users',
                arrayContains: FirebaseAuth.instance.currentUser!.uid,
              )
              .orderBy('lastMessaged', descending: false)
              .get(),

and it is giving me this error

W/Firestore( 4428): (24.2.1) [Firestore]: Listen for Query(target=Query(chats where usersarray_containsOakThEN9ZXcurMnxmzF7H9eOl4J3 order by lastMessaged, __name__);limitType=LIMIT_TO_FIRST) failed: Status{code=FAILED_PRECONDITION, description=The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/instagram-clone-5b92a/firestore/indexes?create_composite=ClNwcm9qZWN0cy9pbnN0YWdyYW0tY2xvbmUtNWI5MmEvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL2NoYXRzL2luZGV4ZXMvXxABGgkKBXVzZXJzGAEaEAoMbGFzdE1lc3NhZ2VkEAEaDAoIX19uYW1lX18QAQ, cause=null}
Jack Duffy
  • 193
  • 12

1 Answers1

2

The error message gives the solution:

The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/instagram-clone-5b92a/firestore/indexes?create_composite=ClNwcm9qZWN0cy9pbnN0YWdyYW0tY2xvbmUtNWI5MmEvZGF0YWJhc2VzLyhkZWZhdWx0KS9jb2xsZWN0aW9uR3JvdXBzL2NoYXRzL2luZGV4ZXMvXxABGgkKBXVzZXJzGAEaEAoMbGFzdE1lc3NhZ2VkEAEaDAoIX19uYW1lX18QAQ

As you can see, the error message contains a URL that you can open to create the missing Firestore index.

More details in the doc, here and here.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • What im trying to understand is why this one is suddenly needing indexes when I have used similar structured querys 2 times before in the project and theyve been fine? any ideas – Jack Duffy Sep 23 '22 at 19:45
  • It is most probably because the previous queries where not compound queries, see the doc referred to in my answer. – Renaud Tarnec Sep 23 '22 at 19:46