getDealTransactionDetailsArtifact
returns an observable, meaning that your code will reach the console.log
before setting this.transactionDetailArtifact = res.data;
.
You need to wait for the observable to emit before using the value it is emitting. Here I have just moved the block of code at the end into the subscribe
. I've duplicated it for the case where data.status !== 'Open'
, but you could refactor that to get rid of the repetition.
edit(data:any){
let dealType = '';
if (data.status === 'Open'){
this.Service.getDealTransactionDetailsArtifact(this.accountId,this.transaction.id).subscribe(
res => {
this.transactionDetailArtifact = res.data;
console.log(" this.transactionDetailArtifact" , this.transactionDetailArtifact)
const state = {
data: {
dataTransaction: this.transactionDetailArtifact
},
}
this.gotoDetails(state);
},
err => {
console.error(err);
}
)
} else {
console.log(" this.transactionDetailArtifact" , this.transactionDetailArtifact)
const state = {
data: {
dataTransaction: this.transaction,
},
}
this.gotoDetails(state);
}
}
I'll leave the code above as it is because it is easier to see how to get it working from your code. You could clean that up in any number of ways. Here, I have extracted a function for the duplicated code, and passed the difference as an argument.
edit(data: any) {
let dealType = '';
if (data.status === 'Open'){
this.Service.getDealTransactionDetailsArtifact(this.accountId,this.transaction.id).subscribe(
res => {
this.transactionDetailArtifact = res.data;
this.handleTransactionChange(this.transactionDetailArtifact)
},
err => {
console.error(err);
}
)
} else {
this.handleTransactionChange(this.transaction)
}
}
handleTransactionChange (transaction: string) {
console.log(" this.transactionDetailArtifact" , this.transactionDetailArtifact)
const state = {
data: {
dataTransaction: transaction,
},
}
this.gotoDetails(state);
}