Order By Multiple Fields Firebase Realtime Database

Viewed 9

I'm trying to filter the data in my Firebase Realtime Database by two different columns ('test1' by equality - to check if it succeeds - and 'firstName' in ascending order) and obtain the first 3 results, but Firebase doesn't allow multiple OrderBy functions in one query. What can I do instead?

The database looks like this:

Donors  {

        John Smith

                     {
                        firstName: "John",
                        lastName: "Smith",
                        categories {
                                     test1 : "success",
                                     test2: "fail"
                                   }
 
                      },
        Jack Smith

                     {
                        firstName: "Jack",
                        lastName: "Smith",
                        categories {
                                     test1 : "success",
                                     test2: "fail"
                                   }
                        
                      },
        Jeff Smith

                     {
                        firstName: "Jeff",
                        lastName: "Smith",
                        categories {
                                     test1 : "success",
                                     test2: "success"
                                   }
                        
                      },
        Josh Smith

                     {
                        firstName: "Josh",
                        lastName: "Smith",
                        categories {
                                     test1 : "fail",
                                     test2: "fail"
                                   }
 
                      }
    }

I've tried building a query to OrderBy both of these, but it returns an error (Uncaught Error: orderByChild: You can't combine multiple orderBy calls)

Here's my code:

const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: "",
measurementId: ""
};

const app = initializeApp(firebaseConfig);
const db = getDatabase(app);

var test = query(ref(db, "/Donors"), orderByChild("/categories/test1"), 
equalTo("success"), orderByChild("firstName"), limitToFirst(3))

get(test).then((snapshot) => {
        console.log(snapshot.val())
    });
0 Answers
Related