I am trying to retrieve data from the current user, specifically their company named "ZeroMax", and then store this data in a global variable. The purpose of this variable is to define the path for Firebase. I believe my code gives a clear understanding of my question. Everything seems to be functioning correctly, except for the assignment
this.comp = user['company'];
Shown below 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];
}
}
Moreover, this is the content of 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('Received undefined');
}
for (let data of res) {
if (data.length > 0) {
this.users.push(data[0]);
//console.log(res);
}
console.log(res);
}
console.log('Operation successful:', this.participant)
this.participant = '';
}, error => {
console.log('An error occurred here: ', error);
});
}
}