3

I am extending the question from the link: Unable to find 'getOpps' on 'compound://c.OpporutnityComponent'. in Lightning Component as I am getting the new error

This page has an error. You might just need to refresh it.
Assertion Failed!: run() cannot be called on a server action. Use $A.enqueueAction() instead. : false
Failing descriptor: {ui:button$controller$press}

enter image description here

OpporutnityLightningAppController.js

({
    getOpps : function(component) {
        var oppt = component.get("c.getOpportunities");

        oopt.setCallback(this, function(response){
            var state = response.getState();
            if(state === 'SUCCESS'){
                component.set("v.opplistAttr", response.getReturnValue());
            }
        });
        $A.enqueueAction(oppt);
    }
})

OpporutnityComponent.cmp

<aura:component controller="OpporutnityLightningController">
    <aura:attribute name="opplistAttr" type="Opportunity[]" />

    <ui:button label="Click Me" press="{!c.getOpportunities}" />
    <aura:iteration var="opt" items="{!v.opplistAttr}">
        <br/> {!opt.Name} <br/>
    </aura:iteration>
</aura:component>

OpporutnityLightningController

public with sharing class OpporutnityLightningController {
    @AuraEnabled
    public static List<Opportunity> getOpportunities(){
        List<Opportunity> oppList = [SELECT Id, Name FROM opportunity];
        return oppList;
    }
}

enter image description here

  • You have a typo in your variable declaration in the javascript controller. – Caspar Harmer Jun 25 '17 at 19:24
3

So what you need to do:

  1. Fix typo in var name
  2. Make sure your button press is calling the javascript action, not the server action
  3. Perhaps remove any getXX type calls on the serverside

Expansion:

Point 1: You declare var oopt... and then you call it oppt - see the difference?

Point 2: You are calling the SERVER action in your component markup. You should be calling your clientside Javascript controller handler instead.

Change

<ui:button label="Click Me" press="{!c.getOpportunities}" />

To:

<ui:button label="Click Me" press="{!c.getOpps}" />

Other things to consider

You should also be passing in three params to the controller method. Eg:

getOpps : function(component, event, helper)

Also, make sure that this is defined in the Controller js file, not the helper js file (ie if you don't have something defined in the Controller file, then you will get this error.

How this works is you will define your handler in markup, ie `press="{!c.getOpps}

Then in your Controller - not helper - you need to define the getOpps function.

  • No, its not working. I already tried that This page has an error. You might just need to refresh it. Unable to find 'getOpps' on 'compound://c.OpporutnityComponent'. Failing descriptor: {markup://c:OpporutnityComponent} – user4567570 Jun 25 '17 at 19:00
  • Well, don't change it away from that - that is definitely the correct syntax. Next thing to try, make sure you have the correct name in the Apex controller - ie getOpportunities is defined. After that, make sure you have the exact controller name - I see a possible typo there too – Caspar Harmer Jun 25 '17 at 19:04
  • Everything looks correct to me.. method name is also used correctly.. – user4567570 Jun 25 '17 at 19:09
  • I have had trouble before (not the same error but similar) when my server side action starts with get. If you change it to instead start with load or init does that help at all? – dsharrison Jun 25 '17 at 19:26
  • Yes, I was experimenting with that too - but it's the typo that's the big one, I think. – Caspar Harmer Jun 25 '17 at 19:27
1

I used the correct names and its working. One typo which I see need change in OpporutnityLightningAppController.js

oopt.setCallback(this, function(response){

to

oppt.setCallback(this, function(response){

Use this

OpportunityLightningApp

<aura:application >
    <c:OpportunityComponent></c:OpportunityComponent>
</aura:application>

OpportunityComponent

<aura:component controller="OpportunityLightningController" access="global" >
    <aura:attribute name="opplistAttr" type="Opportunity[]" />

    <ui:button label="Click Me" press="{!c.getOpps}" />

    <aura:iteration var="opt" items="{!v.opplistAttr}">
        <br/> {!opt.Name} <br/>
    </aura:iteration>
</aura:component>

OpportunityComponentController.js

Here no need to use the event and helper.

({
    getOpps : function(component) {
        var oppt = component.get("c.getOpportunities");
        oppt.setCallback(this, function(response){
            var state = response.getState();
            if(state === 'SUCCESS'){
                component.set("v.opplistAttr", response.getReturnValue());
            }
        });
        $A.enqueueAction(oppt);
    }
})

enter image description here

  • That will only fix one of the issues. You are using part of my answer - user4567570 changed their question to reflect one of my answers. getOpps used to be getOpportunities. – Caspar Harmer Jun 25 '17 at 19:37
  • @CasparHarmer - You wont need getOpps : function(component, event, helper), this will works getOpps : function(component) – user45930 Jun 25 '17 at 19:42
  • Yes, I know. However, generally you should be passing those three params to a controller. – Caspar Harmer Jun 25 '17 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.