Firebase - Filtering data

Hi everyone,

I have a question about Firebase data filtering…

I successfuly added Firebase to my app with authentication, storing and retrieving data and eveything works well. But now i want to optimize my queries.

Currently, i am using this kind of query for retrieving my data :

const userId = this.authService.getActiveUser().uid;
return this.http.get('https://xxxxxxx.firebaseio.com/' + userId + '/data.json?auth=' + token)
  .map((response: Response) => {
    return response.json();
  })
  .do((res: Data[]) => {
    if (res) {
      this.data= res
    } else {
      this.data= [];
    }
  });

}

This have for effect to retrieve all my database and it is not optimized, so i would like to just query on a part of data.

I tried to use Firebase instructions : limitToFirst, limitToLast, startAt, endAt, and equalTo has presented in the documentation here :

https://firebase.google.com/docs/database/rest/retrieve-data

By modifying my query like below :

this.http.get('https://xxxxxxx.firebaseio.com/' + userId + '/data.json?&auth=' + token+'&orderBy="name"&equalTo="a"')

But i always get http bad request error…I don’t understand how to use the Firebase factors in my query. Anybody has an idea ?

Do you realy need the “” around name and a ?

If so you must urldecode() or urlencode() (I always get confused about en and decoding) your url parameters.

Use a Firebase database rule to require automatic indexing of your data according to your field(s) of interest. Then use the Firebase API to return the first X many or last X many items. I don’t see why you need to use this.http. Import either Firebase or AngularFire, inject it as a service, and use the Firebase commands.

According to the official documentation, yes it is required

Not the docs I’ve read.

https://firebase.google.com/docs/database/web/read-and-write

I recently followed a video tutorial on Udemy and the presented solution was using this syntax. Then i have also created new database rules for automatic indexing.

The last solution will be to use AngularFire as a service like you said it.

I have not found the same link (see in my first post)

Try
https://www.w3schools.com/jsref/jsref_encodeuri.asp

To encode your url parameter.

I have finally created a service for my Firebase access like :

    import { Injectable } from '@angular/core';
    import { Http } from '@angular/http';
    import 'rxjs/add/operator/map';
    import firebase from 'firebase';

    @Injectable()
    export class FirebaseService {

      public db: any;

      constructor(public http: Http) { }

      initialize(){

        firebase.initializeApp(
          {
            apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxx",
            authDomain: "xxxxxxx.firebaseapp.com",
            databaseURL: "https://xxxxxxx.firebaseio.com/"
          }
        );

        this.db = firebase.database();
      }
    }

Next i have injected it into my databaseService like below :

import { FirebaseService } from './firebase-service';

export class DatabaseService {

    onFetchData() {
         const userId = this.authService.getActiveUser().uid;
         var ref = this.fbService.db.ref('mydb/' + userId);

         ref.on('value', function(snapshot){
              console.log("[--- Fetched ---] " + snapshot.val());
         }); 
    }
}

But now i always get null in my log for my snapshot object…