Currently, I am in the process of learning angular and attempting to create a component that allows users to register through my web API.
Within my component, the code on form submission looks like this:
onSubmit(contactFormRef: NgForm) {
const result = this.usersService.CreateUser(this.registerDto);
contactFormRef.reset();
}
The UsersService contains the following method:
CreateUser(dto: UserCreateDto): boolean {
const endpoint = ApiQueryService.ApiEndpoint + this.postUserEndpoint;
let result: any;
let d = new Date(dto.birthDate.year, dto.birthDate.month - 1, dto.birthDate.day);
dto.userDto.birthDate = d.toJSON();
const password = dto.password;
this.client.post(endpoint, dto.userDto).subscribe(x =>
{
result = x;
const registerDto: RegisterDto = {Password: password, UserId: result};
const registerResult = this.authService.RegisterUser(registerDto);
return registerResult;
});
return false;
}
and the AuthService contains the following method:
RegisterUser(dto: RegisterDto): boolean {
let isSuccess: boolean = false;
const endpoint = ApiQueryService.ApiEndpoint + this.registerEndpoint;
this.client.post(endpoint, dto, {observe: 'response'}).subscribe(response => {
isSuccess = response.status === 200;
return isSuccess;
});
return isSuccess;
}
My issue arises from wanting to return the isSuccess variable from this line of code within the component:
this.client.post(endpoint, dto, {observe: 'response'}).subscribe(response => {
isSuccess = response.status === 200;
return isSuccess;
});
I aim to handle the result in the component based on whether it is successful or not. However, due to the asynchronous nature of subscribing, the component always receives a false result. This dilemma is hindering the functionality I desire.