samedi 27 juin 2020

Variable not assigning from the first try

I am having issue with assigning value to global variable. When I click button to console my varible it is empty and when I click button second time, I can see my variable value. I do not know why it is empty at first click and not empty at second click. Look at my code below. Thanks!

export class DriversService {
  mc = '';
  dil = '';
  constructor(private db: AngularFirestore, private auth: AuthService, private afAuth: AngularFireAuth) { }

 getUsers() {
  return this.auth.user.pipe(
    take(1),
    map(user => {
      this.mc = user['company'];
      return user['company'];
    })
  )
 }

 getMc(){
   return this.getUsers().subscribe(user => {
     this.mc = user;
     console.log('Hello', this.mc)
     return this.mc;
    });
  }
  
  getComp(){
    this.mc = this.dil;
    if(this.dil.length <= 0){
      return this.getUsers().subscribe(user => {
        this.mc = user;
        console.log('Bye', this.mc)
        return this.mc;
       });
    }
    else{
      console.log('Bye1', this.dil)
    }
  }
  oneMore(){
    console.log(this.mc) // Here it is empty
  }



Aucun commentaire:

Enregistrer un commentaire