I am facing an issue while trying to create a variable using a function from AngularFire2. The function I am using is authorizeUser()
. However, when I try to assign the result of this function to this.user
, the console window displays 'undefined'.
import { Component, OnInit } from '@angular/core';
import { AngularFire } from 'angularfire2';
import { SignupService } from '../../../services/signup/signup.service';
@Component({
selector: 'console',
templateUrl: './console.component.html',
styleUrls: ['./console.component.css']
})
export class ConsoleComponent implements OnInit {
signups: any;
user: any;
constructor(
public af: AngularFire,
private signupService: SignupService
) {}
getSignupsList() {
this.signupService.getSignups().subscribe(signups => {
this.signups = signups;
});
}
authorizeUser() {
this.af.auth.subscribe(afData => {
this.user = afData.auth.email;
});
}
ngOnInit() {
this.getSignupsList();
this.authorizeUser();
console.log(this.user);
}
login() {
this.af.auth.login();
}
}
However, if I log the output within the promise, I am able to see the desired result:
authorizeUser() {
this.af.auth.subscribe(afData => {
this.user = afData.auth.email;
console.log(this.user);
});
}
I am unsure of what I might be missing here. My goal is to assign the value of afData.auth.email
to the global variable user
.