MonishB
MonishB

Reputation: 35

Firebase REST API query with different keys

So this is the structure of my Firebase DB right now, I am using the Firebase REST API:

"company": {
      
   company1_id {
        id: company_id,
        userId: userid,
        name: name
        //someotherstuff
}
    company2_id {
        id: company_id,
        userId: userid,
        name: name,
        //someotherstuff
}

}

Soo, right now I am getting the companies belonging to one user by calling :

"firebasedbname.firebaseio.com/company.json?orderBy="userId"&equalTo=userId"

This works perfectly fine and gets the corresponding data, but now I want it to order the companies alphabetically by name, and then i try this:

"firebasedbname.firebaseio.com/company.json?orderBy="name"&equalTo=userId"

But this time, it returns no data! Even though i have added .indexOn: "name" to the company node.Any help will be aprreciated.

Upvotes: 0

Views: 385

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83068

As explained in the doc, if you want to filter data you need to first "specify how you want your data to be filtered using the orderBy parameter", and then you need to "combine orderBy with any of the other five parameters: limitToFirst, limitToLast, startAt, endAt, and equalTo".

So if you added "an .indexOn: "name" to the company node", it means that you intend to query as follows:

https://xxxx.firebaseio.com/company.json?orderBy="name"&equalTo="companyName"

You cannot order by (company) name and filter on userId.

If you want to get all the companies corresponding to a specific user and order them by the company name, you will need to use ?orderBy="userId"&equalTo=userId" and do the sorting in the client/application calling the REST API.

Upvotes: 1

Related Questions