How to create an array with results obtained through an observableany [] TYPESCRIPT

0

Well, I get an array of keys, which I use to search the firebase:

addprd(a:string){
 this.prdarray.push(a);
}

After adding the keys I want to use in the search I use the following medoto:

queryProd(as:string){
  const prdN :Observable<any[]>= this.db.list('/Produto/${as}').valueChanges();
  return prdN;
}

Returning an observable from this key, how do I make a "for" and store an array of results to display in my HTML?

    
asked by anonymous 04.07.2018 / 12:46

2 answers

1

Do this:

No service:

queryProd(as:string){
  return this.db.list('/Produto/${as}');
}

getItems(ids: string[]): Observable<Item> {
 return from(ids).pipe(
    mergeMap(id => <Observable<Item>> this.queryProd(id ))
  );
}

No Component:

produtos:Produto[] = [];
ids: string[] = [];

constructor(private seuServico:seuServico){}

ngOnInit() {
    this.seuServico.getItems(ids).subscribe(item=>produtos.push(item));            
}

HTML

<div *ngFor='let produto of produtos'>
    {{produto.preco}}
</div>
    
04.07.2018 / 13:46
1

An observable is only a statement of your query to a service, so you actually have access to the data of this observable, you need to give a subscribe.

let prdn = queryProd('batata');

prdn.subscribe(data => {

    // data é o seu array
    for (let item of data) {
        console.log(item.algumDado);
    }
});
    
04.07.2018 / 13:26