MacD
MacD

Reputation: 586

Nested Firebase Query

I am trying to do a nested firebase query for a queue system I'm building. The goal is to get a list of 'stations', then query for 'projects' . that match the given 'station'. then display the data so that my 'stations' are listed with the 'projects' that match that station are listed under that station. My data structure is the following:

projects
   -LYFLi59ZFTcHHNGEl4Y
   -LYFNmlW_fDOxOGep1VT
        SKU: "66534"
        buyerEmail: "[email protected]"
        comments: "comment"
        createdAt: 1549684385662
        queue: 1
        station: "Lathe"
        timeTest: "Fri Feb 08 2019"
stations
   -LYCB7awMyE7gxRKlojN
        StationtName: "Lathe"
   -LYCCBjC4JT9rZlgJSiL
   -LYCCD0J6VqafpRL8Mlf

In this case I'm looking for products that are equalTo "Lathe" and display them in the "Lathe" queue.

Right Now I have a list that returns all my stations:

this.stationRef = firebase.database().ref('stations/');
        this.stationRef.on('value', resp => {
          this.stations = [];
          this.stations = snapshotToArray(resp);

        }); 

Then I have another ref that returns all the projects ordered by stations and equal to the station value in the stations array.

    this.projectRef = firebase.database().ref('projects/')
       .orderByChild('station')
       .equalTo(------this.Is My Problem-----);  ///// Looking for this.stationId //// 
          this.projectRef.on('value', resp => {
            this.projects = [];
            this.projects = snapshotToArray(resp);

        });

I am using the following to return the value of the station I want to pass to the projectRef, but I can't get the variable outside the loop and I cant use the projectRef inside the loop..

    const snapshotToArray = snapshot => {
              let returnArr = [];

              snapshot.forEach(childSnapshot => {
                  let item = childSnapshot.val();
                  item.key = childSnapshot.key;
                  this.stationId = childSnapshot.val().StationtName;
           <--------------------------------------->
                     this.stationId is the 
                     variable I want to pass
                     into the projectRef
           <---------------------------------------->
                  returnArr.push(item);

            });
              return returnArr;
            } 

Thanks in advance for your help! And am I missing any best practices or going about the problem the wrong way?

Thanks again..

Upvotes: 0

Views: 135

Answers (1)

Guru
Guru

Reputation: 296

The Core of Firebase is to completely flatten the data structure(NO-SQL). But at he same time complex queries has been pain point of firebase thats why they are asking us to user firestore for complex query structures.

Here as far as I understand you want to list stations with sub list of its products. The best way to implement it with one sync is Put array of projects id/name in stations. So you only listening to station node and it has array of all project ids and simply show list of stations and use array to show sublist of projects and only tap simply open detail of project using that ID.

stations- id- stationName products:{ prod1, prod2, prod3 }

But if you wanna show all info of products at same screen also just load all products(if its not in millions else move to firestore.) once and save it in map using key and value as product itself and use it to show info. Don't make nested queries in firebase unless you really have to

Upvotes: 1

Related Questions