Hello I have a method in a the class below:
export class SearchService {
userUID: string;
searchItems: any;
private searchesCollection: AngularFirestor
getSearches() {
this.afAuth.authState.subscribe(user => {
this.userUID = user['uid'];
this.searchesCollection = this.db.collection(`users/${this.userUID}/searches`);
this.searchItems = this.searchesCollection.valueChanges().subscribe(data => {
console.log(data); // works
return data;
});
});
console.log(this.searchItems); // undefined
return this.searchItems; //
there is an async call in this. Since you are returning the value before your call is resolved or comes back you will not have data in this.searchItems
since you are using a call to server or to db, use observable to take advantage of Angular's promise concept.