Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

336
Views
Firebase Functions: orderBy function not working

The following code works in the emulator but returns an empty array in firebase when includeVideos or includeAudios is false:

let query = db.collection("content");

if (includeVideos && !includeAudios) {
    query = query.where("type", '==', "video").orderBy("type");
}
if (includeAudios && !includeVideos) {
    query = query.where("type", '==', "audio").orderBy("type");
}
if (!includeVideos && !includeAudios) {
    return {empty json....}
}

if (matchingPersonID != undefined) {
    query = query.where("attributionID", '==', matchingPersonID).orderBy("attributionID");
}

//either categories OR matchingSeriesID is always undefined/empty
if (matchingSeriesID != undefined) {
    query = query.where("seriesIDs", 'array-contains', matchingSeriesID).orderBy("seriesIDs");
}

if (matchingCategories.length != 0) {
    // 'OR' LOGIC
    query = query.where("categories", 'array-contains-any', matchingCategories).orderBy("categories");
}

if (lastDocumentID != undefined) {
    await db.collection("content").doc(lastDocumentID).get().then((snapshot) => {
        query = query.startAfter(snapshot);
        log(`starting after document ${snapshot}`)
    })
}

//the code works when this line is removed, but the query needs to be sorted by time in order for pagination to work
query = query.orderBy("upload_date", "desc");

return query.limit(getNext).get()...

I get no error messages in the google cloud logs, and so I have no easy way of creating an index, if that's what I need to do. In firestore, the fields upload_date, attributionID, type, seriesIDs, and categories are all on the same level in the document.

Any help would be much appreciated!!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As you comment, it seems that an index for that field is missing.

This documentation mentions two ways to handle these indexes:

  • By using the CLI. You can bring your indexes to a local file in a JSON format. For this, run firebase firestore:indexes inside your firebase project folder, you'll get a formatted JSON output for you to copy, add a new one and then deploy them with firebase deploy --only firestore:indexes.

  • Indeed, you can use the URL generated by Firebase when it catches the error of using an index that does not exist, but you can also do it by hand from the Firebase console of the application:

enter image description here

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!