I am working on a JHipster application, I have a Study entity which has a many-to-one relationship with User (So one user can have many studies). I am trying to change the user select in the dialog component for creating a new Study to automatically select the current logged in user.
I have changed the select so that only the single user should show up as an option for the select, after fixing this issue I would either disable the field or remove it so that study.user is automatically set:
<select class="form-control" id="field_user" name="user [(ngModel)]="study.user" >
<option [ngValue]="user.id === study.user?.id ? study.user : user">{{user.login}}</option>
</select>
The userService is queried though:
this.userService.query()
.subscribe((res: ResponseWrapper) => { this.user = res.json; }, (res: ResponseWrapper) => this.onError(res.json));
Which runs this query to the api in user.service.ts:
private resourceUrl = 'api/users';
...
query(req?: any): Observable<ResponseWrapper> {
const options = createRequestOption(req);
return this.http.get(this.resourceUrl, options)
.map((res: Response) => this.convertResponse(res));
}
The GET method in UserResource.java:
@GetMapping("/users")
@Timed
public ResponseEntity<UserDTO> getAllUsers() {
return ResponseUtil.wrapOrNotFound(
userService.getAllManagedUsers()
.map(UserDTO::new));
}
Which calls this method in UserService.java:
@Transactional(readOnly = true)
public Optional<User> getAllManagedUsers() {
return userRepository.findOneByLogin(SecurityUtils.getCurrentUserLogin());
}
findOneByLogin is defined in userRepository, the Spring Data JPA repository for the User entity.
My issue is that when I try to access the user object from my entity, it is undefined, throwing errors such as:
ERROR TypeError: Cannot read property 'id' of undefined