Hello, I am currently developing an angular/typescript application and I have been researching about promises. However, I am still struggling to fully understand it. I would greatly appreciate your assistance.
I have a function called getUserById()
which returns user information, as well as another function called getAttributeByUserId()
. My goal is to populate a form using data from both functions, but the variables in getAttribute
are showing up as undefined
. Below is my code snippet:
Angular / Typescript
getUserById(userId, modalContent) {
console.log('get user by id ' + userId);
const config = AppConfiguration.CONFIG_GET_USERS_BY_ID;
config.user_id = userId;
const date = new Date();
this._httpService.CountUsers().post(config).subscribe((data: any) => {
console.log('resultado al obtener usuario editado ' + data[0].user_id);
this.userForm.patchValue({'firstName': data[0].firstname});
this.userForm.patchValue({'secondName': data[0].secondname});
this.userForm.patchValue({'lastName': data[0].lastname});
this.userForm.patchValue({'mothersLastName': data[0].motherslastname});
this.userForm.patchValue({'birthDay': {
date: {
year: data[0].birthday.substring(0, 4),
month: data[0].birthday.substring(5, 7),
day: data[0].birthday.substring(8, 10)}
}});
this.userForm.patchValue({'roleId': data[0].role_id});
this.userForm.patchValue({'email': data[0].email});
this.userForm.patchValue({'userId': data[0].user_id});
this.open(modalContent);
// this.open(modalContent);
});
}
getAttributeByUserId(userId: number) {
const config = AppConfiguration.CONFIG_GET_ATTRIBUTE_BY_ID;
config.user_id = userId;
this._httpService.CountUsers().post(config).subscribe((data: any) => {
this.attributes = data;
});
}
In order to retrieve the data, I need to call getAttributeByUserId
within getUserById
.