简体   繁体   中英

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?

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.

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