2

I would like to pass two strings to my apex controller, but I keep getting an illegal argument error in my debugger.

jshelper-

    var toDate = cmp.get("v.toDate");
    var dateStr = toDate.toString();
    console.log('date string is', dateStr);
    var addTime = cmp.get("v.statNum");
    console.log('passing time = ', addTime);
    var action = cmp.get("c.getDate");
    action.setParams({"addDate" : dateStr, "addTime" : addTime});

apex controller-

    @AuraEnabled
public static Date getDate(string addDate, string addTime){
    system.debug(addTime);
    system.debug(addDate);
2
  • Can you add more code here .Like your component attribute declaration Jan 24, 2017 at 6:18
  • @Mohith Shrivastava <aura:attribute name="toDate" type="Date" default=""/> <aura:attribute name="statNum" type="String"/> Jan 24, 2017 at 9:03

1 Answer 1

3

ok so this is actually the correct way to pass two arguments from helper to apex. the issue was that I had to convert my addTime to a string (even though in my component it was of type String). working code below

js-

    var toDate = cmp.get("v.toDate");
    var dateStr = toDate.toString();
    var addTime = cmp.get("v.statNum");
    var addTimeStr = addTime.toString();
    var action = cmp.get("c.getDate");
    action.setParams({"addDate" : dateStr, "addTime" : addTimeStr}); 

apex

    @AuraEnabled
    public static Date getDate(string addDate, string addTime){
    system.debug(addTime);
    system.debug(addDate);
5
  • You know, you could have made addDate a date parameter.
    – sfdcfox
    Jan 24, 2017 at 6:17
  • @sfdcfox I tried to do that but it wouldn't pass the date from javascript to apex. it kept returning null. any idea why? Jan 24, 2017 at 9:03
  • Probably because you were using Date.toString(). If you'd left it as a Date, it should have come across okay.
    – sfdcfox
    Jan 24, 2017 at 15:42
  • @sfdcfox my Date.toString was a work around because it kept passing as null. I think maybe js typecasted the Date wrong or something? maybe I should have used let instead of var? or used .get() instead of creating a variable and then passing it. Jan 26, 2017 at 4:39
  • I cannot send a date type. I sent a string and converted it in the apex class
    – m Peixoto
    Jul 1, 2018 at 15:31

You must log in to answer this question.

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