I have an instance of Organisation which initialises with an id:null. In the following code, I detect this condition to determine whether to call create or update on a service:
private saveOrganisation() {
console.log('1 ' + JSON.stringify(this.organisation));
if (this.organisation.id === null) {
// we are creating a new one
this.organisationService.create(this.organisation).subscribe(res => {
console.log('2 ' + res);
console.log('3 ' + JSON.stringify(this.organisation));
return this.organisation = res;
});
} else {
this.organisationService.update(this.organisation).subscribe(res => {return this.organisation = res;});
}
console.log('4 ' + JSON.stringify(this.organisation));
}
I have this instance as a member variable referenced by "this.organisation" in the body of the subscription, i set "this.organisation" with the response.
- The first console log shows this.organisation id : null (as expected)
- The second console log shows res id : 1 (as expected)
- The third console log shows this.organisation id : 1 (as expected)
- The fourth console log shows this.organisation id : null (not expected)
The fourth console.log fires before 2 and 3, while I know that subscribe is asynchronous, i am finding that although I see 2 and 3 in the logs, the id is still never updated, subsequent calls to saveOrganisation() show the null value that I get in 4 (in 1 and 2). I am wondering if this.organisation ever updates.
Can anyone tell me what's going on?