1

Been crash course learning Lightning. Just trying to get a simple apex button to work and having some issues:

my component

<aura:component controller="testjzapex">
      <!--<aura:handler name="init" value="{!this}" action="{!c.doInit}" />-->
    <ui:button label="banana" press="{!c.echo}"/>

      <aura:attribute name="Name" type="String" default="test1" />
    <aura:attribute name="Name2" type="String" default="banana1" />



    <div class="slds">
    <form class="slds-form--inline">
  <div class="slds-form-element">
    <label class="slds-form-element__label" for="name">Name</label>
    <div class="slds-form-element__control">
      <input type="text" id="name" class="slds-input" value = "{!v.Name}" />
    </div>
  </div>
  <div class="slds-form-element">
    <label class="slds-form-element__label" for="email">Email2</label>
    <div class="slds-form-element__control">
      <input type="text" id="email" class="slds-input" value = "{!v.Name2}"  />
    </div>
  </div>
  <div class="slds-form-element">
    <button type="button" class="slds-button slds-button--brand">Send</button>
  </div>

</form>
        </div>
</aura:component>

Controller:

({
    echo : function(cmp) {

       var banana = cmp.get("c.getBanana")
       cmp.set("v.Name2",banana);

    },
})

Apex Class:

public with sharing class testjzapex {
        @AuraEnabled
        public static String getBanana()
        {
            return 'banana';
        }

}

So when I press the button the field is populated with [object Object] instead of 'banana'. I feel like i'm missing something obvious, so any help would be appreciated. Thanks.

3

This is the way we get value from the server and use on client:

var action = cmp.get("c.getBanana");
action.setCallback(this, function(response){
    cmp.set("v.Name2", response.getReturnValue());
});
$A.enqueueAction(action);

So replace the above code for:

var banana = cmp.get("c.getBanana")
cmp.set("v.Name2",banana);

(Invoking a controller method requires the remote call to be queued and the response comes back some time later and is handled by the callback you set.)

  • this worked. Thanks! You are mixing cmp and component though in your answer which gave me an error, but was easily fixed. – J. Zhang Apr 12 '17 at 15:25
1

You are sending a request to the server, however, you do not get the response. Salesforce's documentation here explains how to properly structure the request to the server, based on the example, you can see in the alert, that "banana" is returned after having successfully done the request.

({
    echo : function(cmp) {

       var action = cmp.get("c.getBanana");

        action.setCallback(this, function(response) {
            var state = response.getState();
            // This callback doesn’t reference cmp. If it did,
            // you should run an isValid() check
            //if (cmp.isValid() && state === "SUCCESS") {
            if (state === "SUCCESS") {
                // Alert the user with the value returned 
                // from the server
                alert("From server: " + response.getReturnValue());
                cmp.set("v.Name2", response.getReturnValue());
                // You would typically fire a event here to trigger 
                // client-side notification that the server-side 
                // action is complete
            }
            //else if (cmp.isValid() && state === "INCOMPLETE") {
            else if (state === "INCOMPLETE") {
                // do something
            }
            //else if (cmp.isValid() && state === "ERROR") {
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });

        $A.enqueueAction(action);
    }
})

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.