Looking to create a custom validation in Angular 7.
Here is the code snippet:
CheckUserNameExisit(control: AbstractControl): { [key: string]: any } {
console.log('in functions')
return new Promise(resolve => {
let httpClient: HttpClient;
var userService = new UserService(httpClient);
let usernameExist: Usernameexist;
usernameExist = userService.InitialUserNameExist();
usernameExist.UserName = control.value;
console.log(usernameExist.UserName)
userService.checkUsername(usernameExist)
.subscribe(data => {
if (data.success == false) {
console.log(data.success)
resolve({ 'usernameExists': false });
} else {
console.log(true)
resolve(null);
}
});
});
}
and this is UserSerivce
:
checkUsername(item: Usernameexist): Observable<GenericModel<Usermodel>> {
console.log('go')
return this.httpClient.post<GenericModel<Usermodel>>('https://localhost:44372/api/v1/User/FindbyName/', item);
}
However, when attempting to use this validation, an error occurs:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'post' of undefined TypeError: Cannot read property 'post' of undefined at UserService.push../src/app/services/user.service.ts.UserService.checkUsername
What could be causing this issue? How can it be resolved?