While the console.log
inside the for loop successfully prints project
with the correct updated attributes for both role
and user
within the proposer
object, it seems that once outside of the loop, the changes are not retained. At that point, all I see is an empty project
. How can I ensure that the modified project
data persists beyond the loop?
project: Project = {
course: '',
title: '',
description: '',
proposer: [{role: '', user: ''}],
approved: false,
responsible: [{role: '', user: ''}],
advisor: [{role: '', user: ''}],
examiner: [{role: '', user: ''}],
student: []
};
populateSchema() {
var proposerFound = false;
for (var key in this.project.proposer) {
this.employeeService.getEmployee(this.project.proposer[key].user)
.then((employee: Employee) => {
if (employee != null) {
proposerFound = true;
this.project.proposer[key].role = 'Employee';
this.project.proposer[key].user = employee._id;
console.log(this.project); // updated attributes
}
});
}
console.log(this.project); // NOT updated attributes, empty