Oli
Oli

Reputation: 19

Multiple Query on Firebase Collection

I am querying a firebase collection. I know individually these queries return the 1 record which exist, however together, they do not work. The query should return the one record where the date < the other date and the room equals the room number. The data is there, but does not work when the where statements are both in use:

    return this.afs.collection('/hotels').doc(hid).collection('reservations',ref=>ref
    .where(resdatefromto,'>=',reservationdate)
    .where('room','==',room)
    ).snapshotChanges();
 
   }

What am I doing wrong?

Upvotes: 1

Views: 137

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598718

Most likely you're missing an index. For indexes on multiple fields, you need to have a composite index - which is not automatically created for you.

If this is indeed the problem, check the log output for an error message. In that error message you'll find a link, and clicking that link takes you to the Firebase console with all values to create the correct index pre-populated. All you have to do is click a button, and the necessary composite index will be created for you.


You may also first need to order on the resdatefromto field, before you can perform an >= filter on it. If that is the case, it will also be logged in an error message, and the solution is to call .orderBy(resdatefromto) before using that field in the where clause.

Upvotes: 2

Related Questions