function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dhiraj Gupta 1Dhiraj Gupta 1 

not able to call apex method from controller JS

Hi Team,

I am calling apex class from constrctor but while debugging i am getting below error and cursor is not going inside setCallback method. It seems it is not calling apex class. Below is code:

Error is:
his page has an error. You might just need to refresh it. Action failed: c:BeerExplorer$controller$handleCompEvents [Invalid key search] Failing descriptor: {c:BeerExplorer$controller$handleCompEvents}

Controller code:

({
    handleCompEvents : function(component, event, helper) {
        var searchParam=event.getParam('searchText');
        var action=component.get('search');
        action.setParams({
            txtSearch:searchParam
        });
        
        action.setCallback(this,function(response){
            var state=response.getState();
            if(state=='SUCCESS')
            {
                var responseValue=response.getReturnValue();
                console.log('responseValue',responseValue);
            }
        })
    }
})

Apex Class: 
public class BeerExplorer {

    @AuraEnabled
    public static List<Beer__c> search(string txtSearch)
    {
        string likeParam='%' + txtSearch + '%';
        string query= 'select Id, Name, From Beer__c where Name Like: likeParam ';
        List<Beer__c> beerList=Database.query(query);
        return beerList;
    }
    
}
 
Agustin BAgustin B
Hi Dhiraj, schange :var action=component.get('search'); for var action=component.get('c.search');
Use a console.log(action); to see if you are getting that right.

If it helps please mark as correct, it may help others.
Dhiraj Gupta 1Dhiraj Gupta 1
Hi Agustin,

It worked at this point but it is not able to call apex class. Getting below error:

HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE
Agustin BAgustin B
Hi Dhiraj, from the console.log(action) are you getting information of the c.search method? If not verify that you have the controller class set properly on your class.
I assume the code is incomplete and you enqueue the action to be executed, if not please do so.
$A.enqueueAction(action);

If it helps please mark as correct.
Dhiraj Gupta 1Dhiraj Gupta 1
Hi Agustin,
I  can see below for conole.log(action). Under which i can see c.search method?

getCallback: ƒ ()
getError: ƒ ()
getName: ƒ ()
getParam: ƒ ()
getParams: ƒ ()
getReturnValue: ƒ ()
getState: ƒ ()
isBackground: ƒ ()
setAbortable: ƒ ()
setBackground: ƒ ()
setCallback: ƒ ()
setParam: ƒ ()
setParams: ƒ ()
setStorable: ƒ ()
toString: ƒ ()

Also under setParams i should be able to see property txtSearch but it is not available. 

 
Agustin BAgustin B
well, if you cant set the param it may be that searchParam does not retrieve anything.
Check with console.log(searchParam); 
If you are not getting a String then that might be the error when passing the parameter.
Try debugging that and also put a system.debug(txtSearch) on the apex method to see what you get.
Caleb Kuester 27Caleb Kuester 27
use console.log(JSON.stringify(responseValue));
Dhiraj Gupta 1Dhiraj Gupta 1

Hi,

While calling Apex class, getting response state INCOMPLETE with error net::ERR_CONNECTION_RESET. How to fix this error?