0

I want to retrieve a value from my API. The currently returned value is an array of objects.

[
    {
        "products_id": 793781
    },
    {
        "products_id": 794543
    }
]

I would like to use this data to transform it into a number and thus display the number of products_id returned.

I think the method is going to be .length() but I don't know where to put it.

@Input() archiveVente: [];

ngOnInit() {
    this.ArchiveVente$ = this.dataMlsService.getProducts('Archive', 'Vente');
    console.log(this.ArchiveVente$);
}


getProducts(status, typeTransac):Observable<any> {
    //   console.log(this.baux);
    //   for(this.i=0; this.i<9;this.i++ ){
    let params = new HttpParams();

    params = params.append('status', status);
    params = params.append('typeTransac', typeTransac);

    return this.apiClient.get(this.ApiUrl, { params: params});                
}


<app-compteur [archiveVente]="ArchiveVente$ | async"></app-compteur>
4
  • Try this this.ArchiveVente$ = this.dataMlsService.getProducts('Archive', 'Vente').count(product => product.products_id) ? xgrommx.github.io/rx-book/content/observable/… Commented Jul 7, 2020 at 8:00
  • ERROR TypeError: this.dataMlsService.getProducts(...).count is not a function Commented Jul 7, 2020 at 8:05
  • i look your link Commented Jul 7, 2020 at 8:05
  • If you only have 1 emitted value (since it's HTTP it should be the case) use a map & get the length of your array. Commented Jul 7, 2020 at 8:25

3 Answers 3

2

Simply create a second observable from the first one (archiveVente$), and with map operator, get the length of array emitted inside.

  this.productsCount$ = this.ArchiveVente$.pipe(
    map(products => products?.length)
  );

But to make this working, you should correct your getProducts return type :

getProducts(status, typeTransac):Observable<Product[]> {
  let params = new HttpParams();
  params = params.append('status', status);
  params = params.append('typeTransac', typeTransac);

  return this.apiClient.get<Product[]>(this.ApiUrl, { params: params});
}

And create the Product type.

Sign up to request clarification or add additional context in comments.

1 Comment

why use htppParams to send an object?
0

When ever u think of a transform value to other map is the rxjs function. it transform ur data from one shape to other shape.

so in simple words "u have an array and u want it length instead of that array"

this.productsCount$ = this.ArchiveVente$.pipe(
    map(array => array.length)
  );

Comments

0

HttpParams is NOT for pass parameters. So simply

getProducts(status, typeTransac):Observable<any> {
          const data={status:status,typeTransac:typeTransac}
          return this.apiClient.get(this.ApiUrl,data).pipe(
               map((res:any[])=>{
              //here you has the response, the array, what do you want to do with this?
              //return an object with data and count?
              return {data:res,count:res.length}
              //return only the count?
              return res.length
          }));
           
        }

3 Comments

Hello @Eliseo, the .lenght method doesn't work. The 'length' property does not exist on the 'unknown'.ts(2339) type.
declare the type of the response as ((res:any[])=>{....}, just corrected in code, thanks for the advertisment
Thanks for ur edit. Your solution seems to be error-free, but it still doesn't work. Observable {_isScalar: false, source: Observable, operator: MapOperator} operator: MapOperator {thisArg: undefined, project: ƒ} source: Observable {_isScalar: false, source: Observable, operator: SwitchMapOperator} _isScalar: false proto: Object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.