Daniel Gironás
Daniel Gironás

Reputation: 49

Angular - How to return subscribe data

I'm trying to return the data I get from a query with firease using Ionic and Angular, but the array returns 'undefined'

  QueryList (id: string ) {    

    this.db.collection('/list', ref => ref.where('id', '==', id)).valueChanges().subscribe(ref => {return ref});

  }

What would be the correct syntax to return the array?

Upvotes: 0

Views: 55

Answers (1)

Barremian
Barremian

Reputation: 31105

Return the observable and subscribe where the data is needed

QueryList (id: string ): Observable<any> {    
  return this.db.collection('/list', ref => ref.where('id', '==', id)).valueChanges();
}

Component

ngOnInit() {
  this.someService.QueryList(id).subscribe(ref => {this.ref = ref});
}

The data is asynchronous and it isn't possible to return the value synchronously.

More details on how to access asynchronous data here.

Upvotes: 2

Related Questions