Ich versuche, die Daten von meinem Firebase mit AngularFire2 zu bekommen. Ich möchte bestimmte Daten prüfen und nachdem ich diese Daten von Firebase erhalten habe, kann ich sie nur im bestimmten Umfang und nicht nach der Operation zu Firebase überprüfen. Warum passiert das?AngularFire2 Zugangsdaten nach dem Abrufen
Unten ist mein Code:
this.af.database.list('/users/1qfcMAQnglX9jsW5GdLpPko1HqE2', { preserveSnapshot: true})
.subscribe(snapshots=>{
snapshots.forEach(snapshot => {
if(snapshot.key=="reg_boolean"){
console.log(snapshot.val());
this.bo=snapshot.val();
}
this.currentUser.push({key:snapshot.key,value:snapshot.val()});
console.log(this.currentUser);
//console.log(snapshot.key, snapshot.val());
if(this.bo==true){console.log("happy"); }; //i can access only in this scope
});
})
if(this.bo==true){console.log("happy"); }; //why i can access this value??it's undefined, this happen before the subscribe with angularfire2
Victor Antwort erklärt, aber ich werde verweisen Sie auch auf diese Referenz Fragen: [? Wie kann ich die Antwort von einem asynchronen Aufruf zurückkehren] (http://stackoverflow.com/q/14220321 /), [Variablen, die während der Funktion $ .getJSON gesetzt wurden, die nur innerhalb von function verfügbar sind] (http://Stackoverflow.com/q/1739800) und [jQuery: Daten nach erfolgreichem AJAX-Aufruf zurückgeben] (http://stackoverflow.com)/1/5316697). –