Reputation: 658
I would like to get data from the server using the following method:
searchProducts(): Observable<Product[]> {
return this.http.get<Product[]>('/lerp-product-product');
}
But the data from the server looks like this:
{
"desc": "",
"auth": 0,
"success": 1,
"messages": [],
"obj": null,
"arr": [
{
"product_uuid": "6d80eb05-451e-4309-b0cf-070a14a8e22e",
"product_no": "97508175",
"product_structure": "list",
"product_origin": "intern",
"product_type": "part",
"product_version_extern": "",
"product_revision_extern": "",
}
],
"countProducts": 28
}
The problem is that the data for the observable is in arr
. How can you tell Angular.http
that the data is in arr
and not directly in the root of the JSON response?
Upvotes: 1
Views: 65
Reputation: 1
Add this in yourservice.ts
searchProducts(): Observable<Product[]> {
return this.http.get<Product[]>('/lerp-product-product');
}
Call searchProducts method from yourcomponent.ts
this.yourservice.searchProducts().subscribe(attr => {
var auth= attr.auth;
var success = attr.success;
like these you will get all the values..
});
Upvotes: 0
Reputation: 24134
The standard approach is to use the RXJS map
operator to transform the Observable
HTTP response.
import { map } from 'rxjs/operators';
...
searchProducts(): Observable<Product[]> {
return this.http.get<Product[]>('/lerp-product-product').pipe(map(
response => response.arr
));
}
Upvotes: 1
Reputation: 222582
When you are calling the sevice from your component , you could do access the array arr from the response as follows,
this.myService.searchProducts().subscribe(data=> console.log(data.arr));
Upvotes: 0