How to pass a value from a clientside controller of a lightning component to a serverside controller’s @Auraenabled method?

My purchaseOrderSearchComponent.cmp

<aura:component controller="purchaseOrderSearchControllerRe" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global">
<aura:attribute name="date" type="Date" access="GLOBAL"/>
<aura:handler name="change" value="{!v.date}" action="{!c.dateChange}"/>

<div class="slds-form-element">
           <div class="slds-form-element__control">
             <ui:inputDate aura:id="orderDate" label="Order Date"
                               class="slds-input"
                               labelClass="slds-form-element__label"
                               change="{!c.dateChange}" 
                               value="{!v.date}"
                               displayDatePicker="true"/>
 </div>
 </div>
<div class="slds-form-element">

            <ui:button label="Submit" 
                       class="slds-button slds-button--neutral"
                       labelClass="label"
                       press="{!c.searchPO}"/>
 </div>

My purchaseOrderSearchComponentcontroller.js

 ({
searchPO: function(component, event, helper){
         helper.getPurchaseOrderList(component);

    //Set up the callback

  },
dateChange: function(component, event, helper){
               console.warn("dateChange");
               var date = component.get("v.date");
               console.warn("date is: ", date);
             }
)}

My purchaseOrderSearchComponentHelper.js

    ({
    getPurchaseOrderList: function(component) {

        var ordrdate = component.get("v.date");

        console.log('ordrdate='+ordrdate);
        var action = component.get("c.getSearchList");

        action.setParams({  "ordrdate" : ordrdate });
    //Set up the callback
    var self = this;
    action.setCallback(this, function(actionResult) {
     //do stuffs; 
    });
    $A.enqueueAction(action);
  }
})

My purchaseOrderSearchControllerRe apex class

    @AuraEnabled
  public static List<Purchase_Order__c> getSearchList(Date ordrdate) {

    System.debug('ordrdate='+ordrdate);
}

I am getting ordrdate=null in debug startement. But in console.log statement I am getting a date value.
What wrong am i doing? I have tried a lot to figure out…But all in vain.
Any help!!!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

First of all your controller is not closed properly.
The actual method is

({
    //..............
})

Modify the dateChange methods as below.

dateChange: function(component, event, helper){
        var dateField = component.find("orderDate");
        var date = dateField.get("v.value");
        component.set("v.date", date);
}

Lastly, change the parameter type from Date to String in apex.
It can be converted to date using

Date.valueOf(ordrdate);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x