2

Developing a lightning component for community to display a formula field from custom object to the community page.

getting following error

Component class instance initialization error [Cannot read property 'apply' of undefined] Failing descriptor: {markup://siteforce:designTimeComponent}

Component looks as below

<aura:attribute name="OlympicReward" type="Olympic_Rewards__c[]"/>
<ui:button label="Check Reward status" press="" />
<aura:iteration var="OLRS" items="{!v.OLR}">
    <p>{!OLRS.Reward_level__c}</p>
</aura:iteration>

Component controller

({
getOLS : function(component, event, helper) {
    var action = component.get("c.getOR");
    action.setCallback(this, function(response)){
                       var state =   response.getState();    
    if(state === "SUCCESS"){
        componnet.set("v.OLR", response.getReturnValue());
      }
    });
   $A.enqueueAction(action);
   }
 })

Apex Class

public with sharing class Olympic {

@AuraEnabled
public static Olympic_Rewards__c getOR() {
    Olympic_Rewards__c OLR = 
        [SELECT Id, Name, StatusBar__c,Reward_level__c,Agency_Name__c,Agency__c from Olympic_Rewards__c 
         WHERE 
         Agency__c IN (SELECT AccountId FROM User WHERE Id = :UserInfo.getUserId())LIMIT 1];
        return OLR;
      } 
 }
2

Typically, when I get a apply of undefined error, it's related to a syntax error in my JavaScript.

The way I address this is to run the lightning linter, which is a CLI tool, that's part of the heroku toolbelt. Check out the "Install Lightning CLI" and "Use Lightning CLI" links here: Salesforce Lightning CLI for more information on the linter.

I ran the linter with your javascipt and it pointed out the following syntax error:

FILE: testHelper.js:

error Parsing error: Unexpected token ) Line:4:48 action.setCallback(this, function(response)){

It looks like you have too many ")" right before that curly brace. Fix that, re-run the linter, and then see if your apply of undefined error goes away.

Sometimes these syntax errors also result in "Invalid Pages" in the Napili template too, so just be very diligent about running the lightning linter while you're developing those components.

Edit: Look closer, because your syntax is still incorrect. You also have a few additional typos, including "componnet". The following syntax passed the linter validation:

({
    getOLS : function(component, event, helper) {
        var action = component.get("c.getOR");
        action.setCallback(this, function(response){
            var state =   response.getState();
            if(state === "SUCCESS"){
                component.set("v.OLR", response.getReturnValue());
            }

           $A.enqueueAction(action);
        });
    }
})
  • Thank you for looking into this problem. On that line there are 2 ')' because it requires as you can see. IS there any other line it is indicating? – Aman Jul 13 '17 at 16:12
  • Aman, your syntax is still incorrect. Check my edit above. – James Loghry Jul 13 '17 at 16:26

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.