function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sharukh sk 3Sharukh 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>
Best Answer chosen by Sharukh sk 3
Sharukh sk 3Sharukh sk 3
Got it...
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

Abdul KhatriAbdul Khatri
Hi Sharukh

I don't see any issue with the code but sometimes adding the folloing in @AuraEnabled annotation seems work, not sure why
 
(cacheable=true)
LIke this
public class AttributeTypes {
    public final String[] arrayItems;
    
 @AuraEnabled(cacheable=true)
    public static List<String> getStringArray() {
        String[] arrayItems = new String[]{ 'red', 'green', 'blue' };
        return arrayItems;
    }

}




 
Sharukh sk 3Sharukh sk 3
Got it...
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>
This was selected as the best answer
Sharukh sk 3Sharukh sk 3
Hi Abdul Khatri, without adding (cacheable=true), it works. 
Thanks for your comment.
Abdul KhatriAbdul Khatri
Great, I am glad you found that works for you but your code was working as is in my org. So may be something is going on there.
Ramon MichelRamon Michel
You cannot do DML in Apex Aura Methods declared as cacheable true.
If in LWC , if Aura Method does not have cacheable true you cannot use wire to call apex.