Let me start by acknowledging that while there are numerous questions on this topic, none of them addressed the specific issue I encountered. Despite trying various approaches with maps and other methods, I am still unsure about the correct way to proceed.
My primary goal is to utilize this opportunity to edit the userProfile. The input fields should be preloaded with the existing values from the profile, ensuring that no validation errors occur.
Within the constructor()
of my profile.component.ts
, I invoke the following method:
readProfileData() {
this.userService.getProfile()
.pipe(first())
.subscribe(
userData => {
this.firstname = userData.firstName;
this.lastname = userData.lastName;
this.username = userData.username;
this.email = userData.email;
this.dateOfBirth = userData.dateOfBirth;
this.country = userData.location;
this.profileImage = userData.profileImage;
this.gender = userData.gender;
this.lastUpdated = userData.lastUpdated;
this.activated = userData.activated;
this.userId = userData.userId;
this.userService.profileImageUpdate$.next(this.profileImage);
});
}
The getProfile()
method is then invoked:
getProfile() {
return this.http.get<any>(this.GET_PROFILE_API);
}
In my ngOnInit()
method, I initiate the form building process:
public buildForm() {
this.editForm = this.form.group({
username: [this.username, [Validators.required, Validators.minLength(this.minLength), CustomValidators.validateCharacters], AlreadyTakenValidator.checkUsername(this.registrationService)],
email: [this.email, [Validators.required, Validators.email, CustomValidators.validateCharacters], AlreadyTakenValidator.checkEmail(this.registrationService)],
oldPassword: ['', [Validators.required]],
newPassword: ['', [Validators.required]],
newPasswordConf: ['', [Validators.required]],
firstname: [this.firstname, [Validators.required, NoWhitespaceValidator()]],
lastname: [this.lastname, [Validators.required, NoWhitespaceValidator()]],
country: ['', [Validators.required]],
dateOfBirth: ['', [Validators.required]],
gender: ['', [Validators.required]],
}
, {
validator: MustMatch('newPassword', 'newPasswordConf')
})
}
The challenge at hand is that the input fields remain empty as the values are undefined. Manually entering values before applying the validators works, indicating a problem with the subscription. I need the variables to be populated in order for the fields to display the data.
I have explored solutions involving mapping to res.json or using toPromise, but nothing has proven effective so far. While I understand the asynchronous nature of the issue, I am uncertain how to address it without leaving the user stuck in a loading loop.