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..