rami bin tahin
rami bin tahin

Reputation: 2037

AngularFire2: is it possible to retrieve a specific object from Firebase database by value?

this is my database: firebase database

I want to retrieve a dish that its 'featured' attribute is true (dish.feature = true).

is it possible? or I have to retrieve all dishes and query it on client side??

Upvotes: 1

Views: 789

Answers (1)

Alexander Staroselsky
Alexander Staroselsky

Reputation: 38827

You can use a combination of orderByChild and equalTo to achieve this using AngularFire2 querying lists:

db.list('/dishes', ref => ref.orderByChild('featured').equalTo(true))

The examples uses db for the injected instance of AngularFireDatabase, you would replace that with whatever you named your injected instance in the controller of your component executing the query.

@Component({ ... })
export class FooComponent {
    constructor(db: AngularFireDatabase) {}
} 

Update: You may receive a warning indicating an .indexOn needs to be added to your Firebase database rules. At the most basic level it would look like this:

{
  "rules": {
    "dishes": {
      ".indexOn": ["featured"]
    }
  }
}

Hopefully that helps!

Upvotes: 2

Related Questions