On the Spring JPA side, in my controller, I have:
@GetMapping()
public List<Product> getProducts() {
List<Product> products = repository.findAll();
int a = 1;
return products;
}
In my Angular (6) service, I have:
getAll(): Observable<Product[]> {
return this.http.get<Product[]>(this.PRODUCT_API);
}
In my Angular component, I have:
products: Product[] = [];
ngOnInit() {
this.productService.getAll().subscribe((data: Product[]) => {
console.log(data);
this.products = data;
});
}
In my list component HTML, out of 4 Products in the database, I get the first Product's attributes emitted correctly, but I have nothing in the fields for the other 3.
In the browser's console, I see this:
Array(4) [ {…}, 2, 3, 4 ] product-list.component.ts:19:6
I can put a breakpoint on the bogus assignment in the code running in the Spring controller, and see that I'm returning an array of Product, as expected. But, by the time it leaves Spring and arrives at Angular, the array has become flattened to one object (the first Product), and 3 numbers.
Everything seems to be correctly typed. I can't figure out what I'm supposed to do to return an array of my Product type correctly.
According to the request to show the JSON from the API side, I've finally figured out how to do some basic logging, and dumped the List of Product. Now I'm even more confused. Sure enough, the Angular side is showing exactly what it received:
[ {
"id" : 1,
"title" : "New Friggin Product",
"note" : "This is a note",
"createDateTime" : {
"month" : "AUGUST",
"year" : 2018,
"dayOfMonth" : 20,
"dayOfWeek" : "MONDAY",
"dayOfYear" : 232,
... <SNIP>
"second" : 59,
"monthValue" : 8,
"chronology" : {
"id" : "ISO",
"calendarType" : "iso8601"
}
},
"engine" : 1
} ]
}
}, 2, 3, 4 ]
There's an Array of Product in the JSON, and then the 3 numbers.
Double edit! I had listed the Repository code, but upon further reflection, the repo class is correct. It is returning 4 proper Products in its response.
The problem is the conversion to JSON, and I think I've logged exactly what the Jackson library is doing to the List when I return the response to Angular. The question is why, when everything has been typed correctly.
