You need to sign in to do that
Don't have an account?
Amrut
Accessing data of action.setCallback outside action.setCallback method
var action = component.get("c.fetchData");
action.setCallback(this, function(response) {
var state = response.getState();
console.log("State " + state);
if (state === "SUCCESS") {
var storeResponse = response.getReturnValue();
component.set("v.Customer", storeResponse);
var Credit = storeResponse.field__c;
console.log("Credit "+Credit);
} });
$A.enqueueAction(action);
}
Not able to access "response.getReturnValue();" outside of setcallback, How to do that ?
action.setCallback(this, function(response) {
var state = response.getState();
console.log("State " + state);
if (state === "SUCCESS") {
var storeResponse = response.getReturnValue();
component.set("v.Customer", storeResponse);
var Credit = storeResponse.field__c;
console.log("Credit "+Credit);
} });
$A.enqueueAction(action);
}
Not able to access "response.getReturnValue();" outside of setcallback, How to do that ?
The reason behind getting null value is LIghtning is event driven software architecture.
See, you are assigning the component value inside the setcallback method.
And accessing the value outside it. Thats fine. But the action setCallback method is called based on enqueueAction.
The request is placed in queue. So whats happening is, you are accessing the value first and assigning it latter. Thats why you are getting value as null.
If you place some console log, you will find that console log placed outside the setcallback function are running first and console log inside are running after that.
The solution is to access the value after some time. You can access the component attribute in a helper method. And call that helper method inside the setcallback method. And inside the helper if you try to access the component attribute. It is easily available as it is getting called after the attribute being set.
Hope it helps!
All Answers
https://salesforce.stackexchange.com/questions/171073/lightning-multiple-enqueued-actions-execute-action-when-all-are-done
What you can do is declare a variable outside callback scope and then assign the value to it in the call back. That way you can access that response value anywhere.
Like this:
var responseValue;
var action = component.get("c.fetchData");
action.setCallback(this, function(response) {
var state = response.getState();
console.log("State " + state);
if (state === "SUCCESS") {
var storeResponse = response.getReturnValue();
responseValue=storeResponse;
component.set("v.Customer", storeResponse);
var Credit = storeResponse.field__c;
console.log("Credit "+Credit);
} });
$A.enqueueAction(action);
}
Let me know if that helps.
Thanks
@Piyush. value coming as null by your way
Thanks
The reason behind getting null value is LIghtning is event driven software architecture.
See, you are assigning the component value inside the setcallback method.
And accessing the value outside it. Thats fine. But the action setCallback method is called based on enqueueAction.
The request is placed in queue. So whats happening is, you are accessing the value first and assigning it latter. Thats why you are getting value as null.
If you place some console log, you will find that console log placed outside the setcallback function are running first and console log inside are running after that.
The solution is to access the value after some time. You can access the component attribute in a helper method. And call that helper method inside the setcallback method. And inside the helper if you try to access the component attribute. It is easily available as it is getting called after the attribute being set.
Hope it helps!
Thanks for the responses.