Where() and orderBy() filter not working together when filtering firebase data
Asked Answered
V

3

6

I have a feed which shows users artwork. I want to filter the users posts by the category and time that the user posted it, so that feed will have the latest posts closer to the top of the page. I am using a firebase function to retrieve this data.

I have a firestore collection which looks like this

 tasks -tasksID- 
                {
                   category: art
                   date: 16 october at 3:00pm
                     images: {
                                0 image1
                                1 image2
                             }
                  }

firebase function:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin'

admin.initializeApp()

export const getFeed = functions.https.onCall(async (req,res) =>{
  const docs = await admin.firestore().collection('tasks').where('category', 
    '==', 'art').orderBy('date', 'desc').limit(20).get()    
    return docs.docs.map(doc => {
    return {
        postID: doc.id,
        ...doc.data()
         }
     }) 
 })

art feed typescript:

artFeed (){    
  const getFeed = this.aff.httpsCallable('getFeed')
  this.ajax = getFeed({}).subscribe(data=> {
    console.log(data)
    this.posts =  data
      })  
  }

However, I am getting an error on the console that says "ERROR Error: INTERNAL".

This function works perfectly fine when I use the where() function and orderby() function separately and on their own.

this is what my database indexes look like too.

collectionId      Fields indexed       Query scope            Status 

              category Ascending
tasks         uid Ascending              Collection            Enabled 
              date Ascending


tasks        category Ascending          
             uid Ascending                Collection            Enabled
             date Descending
Varicolored answered 16/10, 2019 at 18:50 Comment(0)
J
6

You need to add a specific index as follows:

collectionId      Fields indexed       Query scope            Status 

              
tasks             category Ascending   Collection             Enabled 
                  date Descending
Jugal answered 16/10, 2019 at 19:40 Comment(0)
F
3

You CAN combine Where() and OrderBy() when using range comparison operators ONLY if you filter on the same field.

As firebase documentation states here:

However, if you have a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field:

  • Valid > citiesRef.where("population", ">", 100000).orderBy("population")
  • Not valid > citiesRef.where("population", ">", 100000).orderBy("country")

https://firebase.google.com/docs/firestore/query-data/order-limit-data

So, you will need to do one operation in the backend and the other in the front-end

Frenzied answered 16/10, 2019 at 19:16 Comment(1)
The OP is using where('category', '==', 'art') (not a range comparison filter) and the query does work after declaring the correct composite index.Jugal
E
1

Had the same issue, But when I was running the application it logs an error in console that query needs to create an compound index and it gave me a link, By clicking that I was able to create and it took couple of minutes to enble. After that it worked.. Hope someone will help

Extensometer answered 18/9, 2021 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.