Roben Wayne
Roben Wayne

Reputation: 31

Get data and make it variable from firebase

I am trying to get data from current user, like company: "ZeroMax", and then assign this data to global variable. And use this variable to clarify the path for firebase. I hope you will understand my question by looking into my code. Everything works just fine, except asssigning

this.comp = user['company'];

This is my drivers.service.ts file:

export class DriversService {

  user: User;
  comp = '';
  constructor(private db: AngularFirestore, private auth: AuthService, private afAuth: AngularFireAuth) { }

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

  findUser(value) {

    if (this.comp.length <= 0) {
      return this.getUsers().pipe(map(user => {
      this.comp = user;
      console.log('Bye', this.comp);
      return this.comp;
      }));
    }

    let email = this.db.collection(`companies/${this.comp}/users`, ref => ref.where('email', '==', value)).valueChanges({ idField: 'id' }).pipe(
      take(1)
    );
    let name = this.db.collection(`companies/${this.comp}/users`, ref => ref.where('name', '==', value)).valueChanges({ idField: 'id' }).pipe(
      take(1)
    );
    return [email, name];
  }
}

And this is my add-driver.page.ts file:

export class AddDriverPage implements OnInit {
 
  users = [];
  participant = '';
  form: FormGroup;

  constructor(private driverService: DriversService, private loadingCtrl: LoadingController, private auth: AuthService) { }

  ngOnInit() {
    
  }

  addDriver() {
    const obs = this.driverService.findUser(this.participant);

    forkJoin(obs).subscribe(res => {
      if (!res || !res.length) {
        return console.log('Got undefined');
      }
      for (let data of res) {
        if (data.length > 0) {
          this.users.push(data[0]);
          //console.log(res);
        }
        console.log(res);
      }
      console.log('it works:', this.participant)
      this.participant = '';
    }, error => {
   console.log('Here you got an error: ', error);
});
  }
}

Upvotes: 0

Views: 66

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598623

It's not where you access the this.comp variable, but when you access it. By the time your this.db.collection(companies/${this.comp}/users`` now runs, the this.comp = user hasn't run yet.

Any code that needs access to the data from the database, needs to be inside the callback that is executed when that data is available.

So for example:

if (this.comp.length <= 0) {
  return this.getUsers().pipe(map(user => {
    let email = this.db.collection(`companies/${user}/users`, ref => ref.where('email', '==', value)).valueChanges({ idField: 'id' }).pipe(
      take(1)
    );
    let name = this.db.collection(`companies/${user}/users`, ref => ref.where('name', '==', value)).valueChanges({ idField: 'id' }).pipe(
      take(1)
    );
    return [email, name];
  }));
}

Upvotes: 1

Related Questions