I'm trying to display the content of HashMap received from my controller, however I get an empty map.
this is my controller:
@PostMapping(value = "getAllUsers")
public HashMap<String, Object> getAllUsers(@RequestBody String s)
{
List<User> list = userRepository.getAll(s);
HashMap<String, Object> userList = new HashMap<String, Object>();
userList.put("list", list);
userList.put("total", list.size());
return userList;
}
and this is my angular code:
getUsers() {
this.userService.getUsers("canada").subscribe((data) => {
console.log(data); // this returns an empty object
data.forEach((value: string, key: string) => { // returns an error which is forEach is not a function
console.log(key, value);
});
this.userList = data; // empty
console.log(this.userList);
});
}
this is my api service:
let URL = "api/v1";
getUsers(country: String): Observable<any> {
return this.http.post(URL + "getAllUsers", country);
}
I want to know how to manipulate and iterate the HashMap received from my controller. Thank you in advance.
http.get()instead ofhttp.post()first read about how http protocols works then use them.List<User> list = userRepository.getAll(s);has data for valuecanada? Also,forEachondatawon't work at front end as its not of typeArray, you need to iterate it overdata.list.