3

I want to display records from custom object based on SOQL query from Apex class. I have referred to this Article which explains how to do that. However I am not able to display records on the component. The following shows the code:

Apex Class:

public with sharing class Olympic {

@AuraEnabled

public static List<Olympic_Rewards__c> Olympic(){

       return [SELECT Id, Name, StatusBar__c,Reward_level__c,Agency_Name__c,Agency__c from Olympic_Rewards__c];

}

}

Component:

<aura:component controller="Olympic" implements="forceCommunity:availableForAllPageTypes" access="global">

<aura:attribute name="OlympicReward" type="Olympic_Rewards__c[]"/>
<!--  <ui:button label="Check Reward status" press="{!c.getMyObjects}" /> -->
<aura:iteration var="OLRS" items="{!v.OlympicReward}">
    <p>{!OLRS.Name},
    {!OLRS.StatusBar__c}</p>
</aura:iteration>

Component Controller:

({
getMyObjects: function(cmp){
var action = cmp.get("c.Olympic");
action.setCallback(this, function(response){
    var state = response.getState();
    if (state === "SUCCESS") {
        cmp.set("v.myObjects", response.getReturnValue());
    }
});
$A.enqueueAction(action);

}

Can someone please help me identify the problem? Above code doesn't display anything while previewing App? No errors.

3 Answers 3

3

Try adding this one, after applying previous suggestion from @mohith-shrivastava

<aura:handler name="init" value="{!this}" action="{!c.getMyObjects}" />

So final result look like:

<aura:component controller="Olympic" implements="forceCommunity:availableForAllPageTypes" access="global">

<aura:attribute name="OlympicReward" type="Olympic_Rewards__c[]"/>
<aura:handler name="init" value="{!this}" action="{!c.getMyObjects}" />
<aura:iteration var="OLRS" items="{!v.OlympicReward}">
    <p>{!OLRS.Name},
    {!OLRS.StatusBar__c}</p>
</aura:iteration>

My guess is - Your button that should populate data is commented out.

0
4

There is a small error there on final object that you are setting.Try with below

({
  getMyObjects: function(cmp){
  var action = cmp.get("c.Olympic");
  action.setCallback(this, function(response){
  var state = response.getState();
  if (state === "SUCCESS") {
     cmp.set("v.OlympicReward", response.getReturnValue());//The attribute that you are iterating has to be set here
   }
 });
 $A.enqueueAction(action);
}
4
  • Thank you @mohith for help. Unfortunately that didn't solve the issue. still not result on app
    – Aman
    Commented Jul 14, 2017 at 14:25
  • 1
    I would recommend use different names for the methods and class .Also use console.log at front end and debug at backend to debug Commented Jul 14, 2017 at 14:27
  • umm I don't understand what is wrong with methods and class name.? Not using reserved keywords am I?
    – Aman
    Commented Jul 14, 2017 at 14:30
  • 1
    Why would you name your methods and class with same names? Also naming convention is not correct but that should not matter .You should keep logs and inspect more Commented Jul 14, 2017 at 14:31
1

Try Posting below code in JS Controller

({
  "getMyObjects" : function(cmp){
  var action = cmp.get("c.Candidate");
  action.setCallback(this, function(response){
  var state = response.getState();
  if (state === "SUCCESS") {
     cmp.set("v.Candidate", response.getReturnValue());//The attribute that you are iterating has to be set here
   }
 });
 $A.enqueueAction(action);
}
})
2
  • Please add some explanation rather than just a code dump.
    – Adrian Larson
    Commented Sep 28, 2017 at 12:23
  • Amogh, welcome to SFSE. You may want to visit the Help center and read "How to Answer". You've answered a 2 month old question that has an "accepted answer" with what we refer to as a "code dump". You've made no effort to explain why your answer is preferable to the accepted answer. I recommend you edit your question to do so.
    – crmprogdev
    Commented Sep 28, 2017 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .