0

I am getting the response value from apex controller but it is not setting in my attribute beerList. what could be wrong? controller

public class BeerSearchAuraService {
    @AuraEnabled
    public static List<sobject> searchBeer(String searchParam){
        String likePAram='%'+searchParam+'%';
        string query='select id, name from Beer__c where name like :likePAram Limit 10';
        List<sobject> sobjectList=Database.query(query);
        return sobjectList;
    }
}

Js controller

({
    handleCompEvent : function(component, event, helper) {
        var searchParam= event.getParam('searchText');
        var action=component.get('c.searchBeer');
        action.setParams({
            searchParam : searchParam
        });
        action.setCallback(this, function(response){
            var state= response.getState();
            if(state==='SUCCESS')
            {
                var responseValue=response.getReturnValue();
                console.log('response value is' ,responseValue);
                component.set('v.beerList',responseValue);
            }
        });
        $A.enqueueAction(action);
    }
})

component

<aura:component implements="force:appHostable" access="global" controller="BeerSearchAuraService" >
    <aura:handler name="BeerEvent" event="c:SearchEvent" action="{!c.handleCompEvent}"/>
    <aura:attribute name="beerList" type="Object"/>
    <div>
    <c:HeaderComponent TagLine="Beer explorer" Message="Search your beer in an easy way" />
        </div>
    <div>
        <c:BeerSearch />
        <aura:iteration items="{!v.beerList}" var="item">
        {!item.id} ---{!item.name}
        </aura:iteration>
        <c:BeerListComponent recordList="{!v.beerList}"/>
        
    </div>
</aura:component>
3
  • Change this <aura:attribute name="beerList" type="Object"/> to <aura:attribute name="beerList" type="List"/> – Nagendra Singh Apr 29 at 3:49
  • Tried. doesnt seem to work. :( @NagendraSingh – Swap31 Apr 29 at 17:05
  • Posted as an answer too. – Nagendra Singh Apr 30 at 1:01
0

Your server is returning a list, and your client seems to be expecting an Object.

0

This is because you have declared, beerList in Aura as Object.

If you are passing List from backend then declare that as List

So changing

<aura:attribute name="beerList" type="Object"/>  

to

<aura:attribute name="beerList" type="List"/>

Also change your return type to List<Beer__c> in Apex code. That should work.

3
  • I have tried changing it to List. But it still didnt work. – Swap31 May 3 at 10:44
  • Can you share apex code for searchBeer? – Nagendra Singh May 3 at 11:09
  • I have shared in the post itself. 1st snippet public class BeerSearchAuraService Have a look – Swap31 May 3 at 11:35
0

Are you testing this as System Admin? A possible reason the user has not read permission of Beer__c object, in Apex you are execution as system context (you have omitted with/out sharing, so by default its without sharing).

To test if the problem is about permissions try this:

@AuraEnabled
public static Object searchBeer(String searchParam){
    String likePAram='%'+searchParam+'%';
    string query='select id, name from Beer__c where name like :likePAram Limit 10';
    List<sobject> sobjectList=Database.query(query);
    return JSON.serialize(sobjectList);
}

and check return value.

If previously this you can see correctly.. -> console.log('response value is' ,responseValue); forget my changes :) and try to change Object -> Map

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.