0

I have the following functions in the aura helper js file:


getSearchTimeframePicklistValues: function(component, event, helper) {
    var action = component.get("c.getSearchTimeframePicklistValues");
    action.setCallback( this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var results = response.getReturnValue();
            component.set("v.searchTimeframes", results.framesList);
            component.set("v.defaultTimeframeId", results.defaultId);
            component.find("searchTimeframePicklist").set("v.value", results.defaultId);
            component.set("v.selectedSearchTimeFrame", results.defaultId);
            window.setTimeout(
                $A.getCallback(this.setStartEndDates(component, event, helper))); 
        }
    });
    $A.enqueueAction(action);
},

setStartEndDates: function(component, event, helper) {
    var searchTimeframes = component.get("v.searchTimeframes");
    var selectedTimeFrame = component.get("v.selectedSearchTimeframe");
    var startingNumberOfDays, endingNumberOfDays;
    for (var i=0; i < searchTimeframes.length; i++) {
        if(selectedTimeFrame == searchTimeframes[i].Id) {
            startingNumberOfDays = searchTimeframes[i].Start__c;
            endingNumberOfDays = searchTimeframes[i].End__c;
            break;
        }
    }
    if(startingNumberOfDays == undefined && endingNumberOfDays == undefined)  {
        component.set("v.startDate", "");
        component.set("v.endDate", ""); 
    }
    else {
        component.set("v.startDate", helper.buildFormattedDate(startingNumberOfDays));
        component.set("v.endDate", helper.buildFormattedDate(endingNumberOfDays)); 
    }   
},

The getSearchTimeframePicklistValues function is called in the doInit function when the component initializes. I'm getting the following error:

AuraError: Assertion Failed!: $A.getCallback(): 'callback' must be a valid Function : false`

Is the syntax wrong? Although I'm getting the error, if I close the error the UI displays the results correctly.

1 Answer 1

0

getCallback() specifically calls this out:

Don't use $A.getCallback() if your code is executed as part of the framework's call stack. For example, your code is handling an event or in the callback for an Apex controller action.

If you want to call the helper function after the apex controller runs, simply call it within the setCallback you've defined for it.

getSearchTimeframePicklistValues: function(component, event, helper) {
    var action = component.get("c.getSearchTimeframePicklistValues");
    action.setCallback( this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {
            var results = response.getReturnValue();
            component.set("v.searchTimeframes", results.framesList);
            component.set("v.defaultTimeframeId", results.defaultId);
            component.find("searchTimeframePicklist").set("v.value", results.defaultId);
            component.set("v.selectedSearchTimeFrame", results.defaultId);
            <!-- call helper method after apex method runs successfully --> 
            this.setStartEndDates(component, event, helper); 
        }
    });
    $A.enqueueAction(action);
},

You must log in to answer this question.

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