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
SAHANA GHOSHSAHANA GHOSH 

Can anyone help on whats missing in the below code ?

Component : 
<aura:component controller="displayColoursClass">
    <aura:attribute name="Colours" type="String[]" default="cyan, yellow, magenta"/>
    <aura:iteration items="{!v.Colours}" var="s">
        {!s}
    </aura:iteration>
    <lightning:button onclick="{!c.getNewList}" label="Update"/>
</aura:component>

Apex  class :
public class displayColoursClass {
    public final  List<String> colourlist;
    @AuraEnabled
    public List<String> getcolornames(){
        colourlist.add('Red');
        colourlist.add('Green');
        colourlist.add('Blue');
        colourlist.add('Yellow');
        system.debug('colourlist : '+colourlist);
        return colourlist;
    }
}
JS Controller :
({
    getNewList : function(component, event, helper) {
        var action = component.get("c.getcolornames");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var stringItems = response.getReturnValue();
                component.set("v.Colours", stringItems);
            }
        });
        
        $A.enqueueAction(action);
    }
})

I am getting the below error when i click on 'Update button'
This page has an error. You might just need to refresh it. Unable to find action 'getcolornames' on the controller of c:displayColours Failing descriptor: {c:displayColours}
Danish HodaDanish Hoda
Hi Sahana,
PFB points to solve this error:
  • The AuraEnabled methods should alwys be static
  • You should give access modifier (public/global) to your lightning component
Please refer below code:
.cmp:

<aura:component controller="displayColoursClass" access="public">
	<aura:attribute name="Colours" type="String[]" default="cyan, yellow, magenta"/>
    <aura:iteration items="{!v.Colours}" var="s">
        {!s}
    </aura:iteration>
    <lightning:button onclick="{!c.getNewList}" label="Update"/>
</aura:component>

.apxc:

public class displayColoursClass {
    @AuraEnabled
    public static List<String> getcolornames(){
        List<String> colourlist = new List<String>();
        colourlist.add('Red');
        colourlist.add('Green');
        colourlist.add('Blue');
        colourlist.add('Yellow');
        system.debug('colourlist : '+colourlist);
        return colourlist;
    }
}