How to create an array with results obtained through an observable<any[]> TYPESCRIPT

Asked

Viewed 295 times

0

Well, I get a key array,:

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;
}

Thus returning an observable of this key,as I can make a "for" and store an array of results to display in my HTML?

  • you have already made a *ngFor in html to test if this is what you wanted?

  • i need the result array to display,queryProd returns the query of a key,i need an array with the result of each key,then I will use *ngFor in html

2 answers

1

An observable is just a statement of your query to a service, for you to actually have access to that observable data, 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);
    }
});

1


Do it like this:

In 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 ))
  );
}

In the 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>
  • But then I won’t just get a product inside of products$:Observable<Product[]>;? i need to store some, after all I have a key array, where each one corresponds to a product

  • If you do not have an array inside the observable, ngFor runs through the array.

  • thus, my queryProd returns a product(in the case of an observable) only,which corresponds to the key that passes by parameter,i have a key array,e would need a corresponding result array

  • You can edit your question then?

  • A doubt the order of the keys matters?

  • Edit yes,can suggest that I edit,the order itself does not,but it needs to be just the ones in a certain array,they correspond to a form,where it selects only a few keys

  • Try my last edition there.

  • ERROR: Cannot find a differ supporting Object '[Object Object]' of type 'Object'. Ngfor only Supports Binding to Iterables such as Arrays.

  • But the array actually ta storing, it’s just not displaying in html

  • tries to initialize the empty array there

  • The type 'Observable<any>' cannot be assigned to the type 'Product[]', db.list returns an Observable<[]{}>

  • Try to put the "| async" and it worked, but only show the last inserted

  • http://prntscr.com/k2h53r

  • yes it will shoot one for each so it has to give a subscribe and push it within the products array, there does not need the async pipe.

  • It worked :),how would I insert this id to the array? for when displaying I have the ID too

  • asks another question if not here will be too long.

  • I already got, anyway thank you,you more that solved,even made me understand how it works :)

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.