I have some problems trying to get the uid out of my constructor. When i run this code the uid shows perfectly in the Test 1 log. However the Test2 log shows its undefined. Does the uid deletes himself when the constructor closes?
import { Component, OnInit } from '@angular/core';
import {AngularFire, FirebaseObjectObservable, FirebaseListObservable} from 'angularfire2';
@Component({
selector: 'dashboard',
templateUrl: 'dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent {
userid:string;
users:FirebaseListObservable<any[]>;
constructor(public af: AngularFire) {
this.af.auth.subscribe(auth => {
console.log(auth.uid);
this.userid = auth.uid;
console.log("Test 1 "+ this.userid);
});
console.log("Test2 "+ this.userid);
}
}
this is my output in my console
Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
dashboard.component.ts:18 Test2 undefined
dashboard.component.ts:14 CgTltJ1h2TUFS5teoACCxoPHP1g1
dashboard.component.ts:16 Test 1 CgTltJ1h2TUFS5teoACCxoPHP1g1
My second question is how can i get the uid out of my constructor? Cause i need it to get some othre data from the database.
When console.log("Test2...")
is executed the value is not yet available
When the data arrives from the server the function passed to subscribe()
auth => {
console.log(auth.uid);
this.userid = auth.uid;
console.log("Test 1 "+ this.userid);
}
is called, only then the data becomes available and only within that function, console.log("Test2 ...")
was executed a looong time before that
export class DashboardComponent {
userid:string;
users:FirebaseListObservable<any[]>;
constructor(public af: AngularFire) {
this.af.auth.subscribe(auth => {
console.log(auth.uid);
this.userid = auth.uid;
console.log("Test 1 "+ this.userid);
});
console.log("Test2 "+ this.userid);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With