I need to retrieve the number of users from a table called Users and display it on the frontend using Angular. Below are my methods that return the number of users on the backend:
UserRepo.Java
@Query(value="SELECT COUNT(*) FROM User")
int getUsersCount();
UserController.Java
@GetMapping("/countUsers")
@PreAuthorize("hasRole('ADMIN')")
public int countUsers() {
return UserService.getUsersCount();
}
The issue I am facing is that I am unable to retrieve the result as a number on the frontend, it always shows as undefined. Here is how I am calling my method on the frontend:
UserService.ts
countUsers() {
return this.http.get(`${AppConstants.API_URL}`+'countUsers'); }
UserComp.ts
public count(){
this.userService.countUsers().subscribe(
);
When I try to display the result with this.count(), it returns undefined. Additionally, when I attempt to change the return type to number in the service part, I get an error stating:
Type 'Observable' is not assignable to type 'number'