How to catch a property of a JSON object from an Angular HTTP call and fix the "No property" error in Angular CLI? - json

I am using a public API to fetch movie data. And the following is my service method for getting that data from API:
getMovieList(): Observable<Movie[]> {
return this.http.get(this.moviesURL).pipe(
map((data: Movie[]) => data),
catchError(this.handleError),
);
}
And this is the method in the component for subscribing that data:
getMovieList(): void {
this.movieApiService.getMovieList()
.subscribe(
(data: Movie[]) => {
this.movieList = data.results;
}
);
}
The problem is that the API returns an object which has 4 properties: page, results, total_pages, total_results. And I only need the results property. But when I try to assign data.results to my component's property or send data.results from my service method instead of data then angular cli gives an error of "results is an undefined property of data". My question is how do I get the results property directly without having to touch the data object and i also need to assign Movie[] type to the results. But currently I am setting the type to the data object.

The problem lies in your model, you defined that you expect array of Movies but you receive the object with 4 properties which one of them called results are the model you defined, so the solution is:
Define the interface like this:
export interface IDataFromApi {
results: Movie[];
page: number;
total_pages: number;
total_results: number;
}
Then the first function will be:
getMovieList(): Observable<IDataFromApi> {
return this.http.get(this.moviesURL).pipe(
map((data: IDataFromApi) => data),
catchError(this.handleError),
);
And method in component:
getMovieList(): void {
this.movieApiService.getMovieList()
.subscribe(
(data: IDataFromApi) => {
this.movieList = data.results;
}
);
}

Related

Angular array remains undefined when passing JSON data

I have an API which returns JSON data about football. The data is then passed to the frontend (angular) but when passing it to the array, the array is still remaining undefined.
JSON Data:
match_id":"194200",
"country_id":"41",
"country_name":"England",
"league_id":"148",
"league_name":"Premier League",
"match_date":"2019-04-01",
"match_status":"Finished",
"match_time":"21:00",
"match_hometeam_id":"2617",
"match_hometeam_name":"Arsenal",
"match_hometeam_score":"2 ",
"match_awayteam_name":"Newcastle",
"match_awayteam_id":"2630",
"match_awayteam_score":" 0",
This is the angular code to parse the JSON data and put in the array to display:
export class ResultComponent implements OnInit {
searchFilter: string;
resultArr: FootballModel[];
constructor(private footballService: FootballService, private route:
ActivatedRoute) {}
ngOnInit() {
this.footballService.getResults().subscribe(x => this.resultArr = x);
console.log(this.resultArr);
}
When I console.log the x passed in subscribe, the JSON information is returned. So till the x part it is passing well but when it is passing to resultArray and console.log that part, it is returning undefined. Wonder if anyone can help.
This is the model:
export class FootballModel {
countryName: string;
leagueName: string;
matchDate: string;
matchHomeTeamName: string;
matchAwayTeamName: string;
matchHomeTeamScore: string;
matchAwayTeamScore: string;
}
EDIT:
Also I am trying to display that data in a table, but somehow it is not showing. Pretty sure it's an easy mistake as well.
<tbody>
<tr *ngFor="let result of results">
<td>{{result.countryName}}</td>
<td>{{result.leagueName}}</td>
<td>{{result.matchDate}}</td>
<td>{{result.homeTeam}}</td>
<td>{{result.awayTeam}}</td>
<td>{{result.homeTeamScore}}</td>
<td>{{result.awayTeamScore}}</td>
</tr>
Http requests return Observable on Angular. Observables has async callback function and you can get data by subscribing it as you did. But when you try to reach data outside of callback function before .subscribe worked at least one time it must be undefined. Because it is writing to the console before your API send response. If you change your ngOnInit function like that it must work.
ngOnInit() {
this.footballService.getResults().subscribe(x => {
this.resultArr = x;
console.log(this.resultArr);
});
}
Also check the documentation for Observables
Here is an additional example for this case:
ngOnInit() {
console.log("a");
this.footballService.getResults().subscribe(x => {
console.log("c");
this.resultArr = x;
console.log(this.resultArr);
});
console.log("b");
}
Expected result on console is
"a" "b" "c"

How to map json response to model

games json response
I originally had this piece of code
this.gameService.getAll()
.pipe(map(data => data['hydra:member']))
.subscribe( (games: Game[]) => this.games = games );
but if I want to retrieve more properties from the json response someone told me to do this
this.gameService.getAll()
.pipe(map(data => data['hydra:member']))
.subscribe(result => [result["hydra:member"], result['hydra:totalItems']]);
however how do you add the following to the above code:
(games: Game[]) => this.games = games );
and map totalItems to a variable.
with this line: pipe(map(data => data['hydra:member'])), it will only contain data from hydra:member.
Is this.games contain all data from API or the hydra:member?
try this:
this.gameService.getAll().subscribe((games: Game[]) => {
this.games = games;
this.totalItems = games['hydra:totalItems']
});
I would try defining my payload (the object I plan to return from the call) and fill that object out using type safety to help me out.
Something like this.
// you can just add more properties later if you like
export interface IGamesGetAllResponseObject {
games: any[]; // or better yet define the game object so it isn't any
totalGames: number;
}
// now define a function that will map the response to the intended object
export mapGamesGetAllToReponse(data): IGamesGetAllResponseObject {
return {
games: data['hydra:member'],
totalGames: data['hydra:totalItems'],
};
}
...
//then use it like this
this.gameService.getAll()
.pipe(map(mapGamesGetAllToReponse))
.subscribe( (result: IGamesGetAllResponseObject) => {
this.games = result.games;
this.totalGames = result.totalGames;
});
I haven't checked all my syntax there 100% but it should be very close to a solution for you.
I would also consider doing all that mapping in the service itself, rather than where you are subscribing. By doing that, you can contain the logic about what data you want and how to map it to the service, and the component just requests what it wants.
you can get multi property from the json response by call same property so that the above code will be :
this.gameService.getAll().pipe(map(result =>
{
this.games = result["hydra:member"] ;
this.totalItems = result['hydra:totalItems'];
}
)).subscribe();
and then call 'this.games' and 'this.totalItems' inline subscribe block.

Why undefined is displayed instead of object - (Angular HTTP request)

I want to get an array from Spring Boot API and I can't convert data into object properly
It's my model:
export class Uczen {
constructor(
public firstname: string,
public lastname: string,
public email: string,
public gender: string,
) { }
}
service:
getPersons() {
return this.http.get('http://localhost:8080/myApp/persons');
}
It's my component.ts code
osoby: Uczen[] = [];
ngOnInit() {
this.personService.getPersons().subscribe((result: Uczen[]) => {
for (const item of result) {
this.osoby.push(item);
}
});
console.log(this.osoby[1]);
console.log(this.osoby.length);
}
im getting "undefined" and "0" as display,there is problem with conversion json to object array prodably ;/
consoles should be inside the subscription since this is an asynchronous procedure
this.personService.getPersons().subscribe((result: Uczen[]) => {
for (const item of result) {
this.osoby.push(item);
}
console.log(this.osoby[1]);
console.log(this.osoby.length);
});
In your ts file, you have ngOnInit() method, ngOnInit is component's life cycle hook which runs when component is being initialized.
1) Initially the control calls the getPersons() method and it will take some time (some milli seconds) to get response from your server.
2)Due to asynchronous behaviour, before we get response from remote server the control goes to the console statement line and it is executed.
At this point of time the variable osoby is still an empty array, which is why you are getting undefined , accessing first element of empty array.
3)If you write the same console lines inside subscription, the control to those lines will go only after receiving the response from your server .
this.personService.getPersons().subscribe((result: Uczen[]) => {
for (const item of result) {
this.osoby.push(item);
}
console.log(this.osoby[1]);
console.log(this.osoby.length);
});

Deserializing json in Angular 2/4 using HttpClientModule

So I'm getting the following JSON structure from my asp.net core api:
{
"contentType": null,
"serializerSettings": null,
"statusCode": null,
"value": {
"productName": "Test",
"shortDescription": "Test 123",
"imageUri": "https://bla.com/bla",
"productCode": null,
"continuationToken": null
}
}
I have the following typescript function that invokes the API to get the above response:
public externalProduct: ProductVM;
getProductExternal(code: string): Observable<ProductVM> {
return this.http.get("api/product?productCode=" + code)
.map((data: ProductVM) => {
this.externalProduct = data; //not working...
console.log("DATA: " + data);
console.log("DATA: " + data['value']);
return data;
});
}
ProductVM:
export interface ProductVM {
productName: string;
shortDescription: string;
imageUri: string;
productCode: string;
continuationToken: string;
}
My problem is that I can't deserialize it to ProductVM. The console logs just produce [object Object]
How can I actually map the contents of the value in my json response to a ProductVM object?
Is it wrong to say that data is a ProductVM in the map function? I have tried lots of different combinations but I cannot get it to work!
I'm unsure whether I can somehow automatically tell angular to map the value array in the json response to a ProductVM object or if I should provide a constructor to the ProductVM class (it's an interface right now), and extract the specific values in the json manually?
The data object in the map method chained to http is considered a Object typed object. This type does not have the value member that you need to access and therefore, the type checker is not happy with it.
Objects that are typed (that are not any) can only be assigned to untyped objects or objects of the exact same type. Here, your data is of type Object and cannot be assigned to another object of type ProductVM.
One solution to bypass type checking is to cast your data object to a any untyped object. This will allow access to any method or member just like plain old Javascript.
getProductExternal(code: string): Observable<ProductVM> {
return this.http.get("api/product?productCode=" + code)
.map((data: any) => this.externalProduct = data.value);
}
Another solution is to change your API so that data can deliver its content with data.json(). That way, you won't have to bypass type checking since the json() method returns an untyped value.
Be carefull though as your any object wil not have methods of the ProductVM if you ever add them in the future. You will need to manually create an instance with new ProductVM() and Object.assign on it to gain access to the methods.
From angular documentation: Typechecking http response
You have to set the type of returned data when using new httpClient ( since angular 4.3 ) => this.http.get<ProductVM>(...
public externalProduct: ProductVM;
getProductExternal(code: string): Observable<ProductVM> {
return this.http.get<ProductVM>("api/product?productCode=" + code)
.map((data: ProductVM) => {
this.externalProduct = data; // should be allowed by typescript now
return data;
});
}
thus typescript should leave you in peace
Have you tried to replace
this.externalProduct = data;
with
this.externalProduct = data.json();
Hope it helps
getProductExternal(code: string): Observable<ProductVM> {
return this.http.get("api/product?productCode=" + code)
.map(data => {
this.externalProduct = <ProductVM>data;
console.log("DATA: " + this.externalProduct);
return data;
});
}
So, first we convert the response into a JSON.
I store it into response just to make it cleaner. Then, we have to navigate to value, because in your data value is the object that corresponds to ProductVM.
I would do it like this though:
Service
getProductExternal(code: string): Observable<ProductVM> {
return this.http.get(`api/product?productCode=${code}`)
.map(data => <ProductVM>data)
.catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}
Component
this.subscription = this.myService.getProductExternal(code).subscribe(
product => this.externalProduct = product,
error => console.warn(error)
);
I used this approach in a client which uses the method
HttpClient.get<GENERIC>(...).
Now it is working. Anyway, I do not understand, why I do not receive a type of T back from the http client, if I don't use the solution provided in the answer above.
Here is the client:
// get
get<T>(url: string, params?: [{key: string, value: string}]): Observable<T> {
var requestParams = new HttpParams()
if (params != undefined) {
for (var kvp of params) {
params.push(kvp);
}
}
return this.httpClient.get<T>(url, {
observe: 'body',
headers: this.authHeaders,
params: requestParams
}).pipe(
map(
res => <T>res
)
);
}

Angular: Typescript casting JSON response as object model not working

I have an issue while I try to cast a json response to object, all the properties of my object are string is that normal ?
Here is my ajax request :
public getSingle = (keys: any[]): Observable<Badge> => {
return this._http.get(this.actionUrl + this.getKeysUrl(keys))
.map((response: Response) => response.json() as Badge )
.catch(this.handleError);
}
Here is my badge model :
export interface Badge {
badgeNumber: number;
authorizationLevel: number;
endOfValidity: Date;
}
And here is where I call the service function and I'm facing the issue :
this._badgeService.getSingle(this.ids).subscribe(
(badge: Badge) => {
console.log(typeof(badge.endOfValidity)); // <-- returning string and not Date
},
error => console.log(error);
});
Thats kinda tricky to explain:
Date is a class, this means that values of type Date need to be created through a constructor call. In other words, create a class instance with new Date(...).
The Response.json method will only return an object in JSON format, and such doesnt contain an instance of any class, only maps of key:property.
So what you need to do, is to manually convert the value returned from .json() to a Base object. This can be done as follows:
public getSingle = (keys: any[]): Observable<Badge> => {
return this._http.get(this.actionUrl + this.getKeysUrl(keys))
.map(r => r.json())
.map(v => <Badge>{
badgeNumber: v.badgeNumber,
authorizationLevel: v.authorizationLevel,
endOfValidity: new Date(v.endOfValidity)
// preferably this string should be in ISO-8601 format
})
//the mapping step can be done in other ways most likely
.catch(this.handleError);
}