In my Angular2 component constructor I store a value from Firebase into a variable.
this.dbAction.getDB().take(1).subscribe(data => {
this.userVisitOrder = data[0][this.currentUserID]['settings']['visitOrder'];
console.log(this.userVisitOrder); // Value exists
});
I need exactly that variable to build my Observable to use specific firebase data. Also in my constructor:
this.visitsRef = afDatabase.list('data/users/' + this.currentUserID + '/visits/', ref => ref.orderByChild(this.userVisitOrder)); // Here the value is undefined
I think that´s an async issue, but how can I access the data stored in my variable?
The getDb()function in my dbAction Service looks like that:
getDB() {
return this.afDatabase.list(`data`).valueChanges().map((data) => data);
}
And if I try to put the second code into the first like that:
this.dbAction.getDB().take(1).subscribe(data => {
this.userVisitOrder = data[0][this.currentUserID]['settings']['visitOrder'];
this.visitsRef = afDatabase.list('data/users/' + this.currentUserID + '/visits/', ref => ref.orderByChild(this.userVisitOrder));
this.visits = this.visitsRef.snapshotChanges().map(changes => {
return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
});
});
... I get the following console error:

getDb()helper function from my service. :)