Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.

15 St Margarets, NY 10033
(+381) 11 123 4567



Firebase Querying Using AngularJS – DZone Mobile

I’ve already done a couple of posts about Firebase and how to use the real-time database and storage with AngularJS. If you missed it, you can check it out here. Today, I want to go further and give you some details on how to querying.

Understanding the Reference Object

As you already know, we can define a reference object just by calling the following functions in the database object: firebase.database().ref(). This reference object offers you a bunch of functions to query our database paths in order to return just a subset of objects.

Using Orderbychild(), Orderbykey(), Orderbypriority(), and Orderbyvalue()

Using this set of functions allows us to create a new query reference that will be sorted by different properties. For example, child will sort the subset by a specific property within the returned data and key is going to sort the subset by the object key.

Using Startat(), Endat(), and Equalto()

Using this set of functions allow us to generate a query with a subset of an object that matches with the specified conditions. For example, we can use equalTo chained with orderByChild in order to sort the subset by a specific property and then filter this subset to the results that match with an specific condition, as I did in the example below.

function getPlacesByUser(uid) {
    if (!places) {
        places = $firebaseArray(firebaseDataService.places.orderByChild('userId').equalTo(uid));
    return places;

Applying those functions, as you can see, makes it really easy to query our database in order to get the specific results we need. I have a working example in this link if you want to see the complete implementation. In this example, I filter the result by user ID.

It’s important to highlight the reference object only accepts one orberByChild function in the chain. That means that you can’t use it twice to filter the subset by two different properties. If you want to do that, you need to add a property to the object which is a combination of the properties you want to filter.

For a complete reference of Angular Fire, use this link. For a complete reference of Firebase, use this link. Finally, for a complete list of querying functions, you can use this link.

If you found this post useful, please don’t forget to press the like button and share it. If you are in doubt, don’t hesitate to ask a question. As always, thank you for reading.

Credit: Source link

Previous Next
Test Caption
Test Description goes like this