2

I am not getting server side values in my javascript.

Markup

<aura:component controller="ContactListController">

    <aura:attribute name="contacts" type="Contact[]"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:iteration items="{!v.contacts}" var="contact">
        <p>{!contact.Name}</p>
        <p>{!contact.Phone}</p>
    </aura:iteration>
</aura:component>

Script

({
    doInit : function(component, event) {
        var action = component.get("c.findAll");
        action.setCallback(this, function(a) {
            component.set("v.contacts", a.getReturnValue());
        });
        $A.enqueueAction(action);
    }

Apex

public with sharing class ContactListController {
    public static List<Contact> findAll() {
        return [SELECT id, name, phone FROM Contact LIMIT 50];
    }
}
Adrian Larson
  • 149,971
  • 38
  • 239
  • 420
Srikanth
  • 23
  • 2
  • 2
    what does a.getReturnValue() return? you should at least try to debug a bit before asking. Also case sensitivity is important, contact.Name != contact.name – glls Dec 15 '17 at 17:34

2 Answers2

3

You have missed @AuraEnabled notation in your server-side(Apex Controller)

  public with sharing class ContactListController {
   @AuraEnabled
  public static List<Contact> findAll() {
    return [SELECT id, name, phone FROM Contact LIMIT 50];}}

The @AuraEnabled annotation enables client- and server-side access to an Apex controller method. Providing this annotation makes your methods available to your Lightning components.

SFDC Learner
  • 2,344
  • 3
  • 37
  • 85
0

You might also want to add some error handling in your code, this way, you wont end up posting questions here every time you run into something --> Throwing and Handling Errors

  function(components, status, errorMessage){
                if (status === "SUCCESS") {

                    //DO SOMETHING
                }
                else if (status === "INCOMPLETE") {
                    console.log("No response from server or client is offline.")
                    // Show offline error
                }
                else if (status === "ERROR") {
                    console.log("Error: " + errorMessage);
                    // Show error message
                }

for future reference: You should check How do I debug my lightning component?

Also as mentioned in my comment, case sensitivity is important in Javascript. Happy Coding!

glls
  • 20,137
  • 19
  • 46
  • 82