typescript function returning undefined

前端 未结 2 716
星月不相逢
星月不相逢 2020-12-12 02:45

Hello I have a method in a the class below:

export class SearchService {
  userUID: string;
  searchItems: any;
  private searchesCollection: AngularFirestor         


        
2条回答
  •  甜味超标
    2020-12-12 03:25

     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.

提交回复
热议问题