'i get the Data just in subscribe bracket [closed]
This is My Code: getTransaction(transactionRef: string){
this.partnerportalService
.getTransaction(transactionRef)
.subscribe(data=>{
this.transactionData = data;
console.log("Return the Data ", this.transactionData);
});
console.log("Return Undefined", this.transactionData);
}
Solution 1:[1]
.subscribe
is asynchronous, meaning that it is going to be executed sometime later, however the line that contains console.log("Return Undefined", this.transactionData);
is outside of the subscribe
function scope and is instead executed synchronously. This means that console.log("Return Undefined", this.transactionData);
will execute before this.transactionData = data;
, hence it prints undefined
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |