You need to sign in to do that
Don't have an account?
Sharukh sk 3
Retrieving Data from an Apex Controller
Hi, I am getting below Error for below mentioned codes.
"This page has an error. You might just need to refresh it. Unable to find action 'getStringArray' on the controller of c:ApexComponent Failing descriptor: {c:ApexComponent}"
Apex Class:
public class AttributeTypes {
public final String[] arrayItems;
@AuraEnabled
public static List<String> getStringArray() {
String[] arrayItems = new String[]{ 'red', 'green', 'blue' };
return arrayItems;
}
}
ApexComponent.cmp:
<aura:component controller="AttributeTypes">
<aura:attribute name="favoriteColors" type="String[]" default="cyan, yellow, magenta"/>
<aura:iteration items="{!v.favoriteColors}" var="s">
{!s}
</aura:iteration>
<lightning:button onclick="{!c.getString}" label="Update"/>
</aura:component>
ApexComponentController.js:
({
getString : function(component, event) {
var action = component.get("c.getStringArray");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var stringItems = response.getReturnValue();
component.set("v.favoriteColors", stringItems);
}
});
$A.enqueueAction(action);
}
})
ApexApplication.app:
<aura:application >
<c:ApexComponent/>
</aura:application>
"This page has an error. You might just need to refresh it. Unable to find action 'getStringArray' on the controller of c:ApexComponent Failing descriptor: {c:ApexComponent}"
Apex Class:
public class AttributeTypes {
public final String[] arrayItems;
@AuraEnabled
public static List<String> getStringArray() {
String[] arrayItems = new String[]{ 'red', 'green', 'blue' };
return arrayItems;
}
}
ApexComponent.cmp:
<aura:component controller="AttributeTypes">
<aura:attribute name="favoriteColors" type="String[]" default="cyan, yellow, magenta"/>
<aura:iteration items="{!v.favoriteColors}" var="s">
{!s}
</aura:iteration>
<lightning:button onclick="{!c.getString}" label="Update"/>
</aura:component>
ApexComponentController.js:
({
getString : function(component, event) {
var action = component.get("c.getStringArray");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var stringItems = response.getReturnValue();
component.set("v.favoriteColors", stringItems);
}
});
$A.enqueueAction(action);
}
})
ApexApplication.app:
<aura:application >
<c:ApexComponent/>
</aura:application>
We need to add <br/> in line no. 4 after {!s}..
<aura:component controller="AttributeTypes">
<aura:attribute name="favoriteColors" type="String[]" default="cyan, yellow, magenta"/>
<aura:iteration items="{!v.favoriteColors}" var="s">
{!s} <br/>
</aura:iteration>
<lightning:button onclick="{!c.getString}" label="Update"/>
</aura:component>
All Answers
I don't see any issue with the code but sometimes adding the folloing in @AuraEnabled annotation seems work, not sure why
LIke this
We need to add <br/> in line no. 4 after {!s}..
<aura:component controller="AttributeTypes">
<aura:attribute name="favoriteColors" type="String[]" default="cyan, yellow, magenta"/>
<aura:iteration items="{!v.favoriteColors}" var="s">
{!s} <br/>
</aura:iteration>
<lightning:button onclick="{!c.getString}" label="Update"/>
</aura:component>
Thanks for your comment.
If in LWC , if Aura Method does not have cacheable true you cannot use wire to call apex.