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
Vinay Ramu 16Vinay Ramu 16 

Unable to set Aura Component Attribute from Helper

Hi All,

I was working on aura component specialist challenge however, faced problem in setting component attribute value in helper JS. Need help on how I can set Boat__c[] type attribute in BoatSearchResults component.
BoatSearchResults.cmp
<aura:component controller="BoatSearchResults" >
    <aura:attribute name="boats" type="Boat__c[]"/>
    <aura:attribute name="boatTypeId" type="Id" />
    <aura:attribute name="selectedBoatId" type="String" />
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <aura:handler name="boatselect" event="c:BoatSelect" action="{!c.onBoatSelect}" />
    <aura:handler name="boatselected" event="c:BoatSelected" action="{!c.onBoatSelected}" />
    
    <aura:method name="search" action="{!c.doSearch}" description="Takes boatTypeId as input parameter"> 
        <aura:attribute name="boatTypeId" type="Id" /> 
    </aura:method>
    <lightning:layout horizontalAlign="space" multipleRows="true">
        <lightning:layoutItem size="9" padding="around-small">
            <aura:iteration items="{!boats}" var="boat">
                <c:BoatTile boat="{!boat}" selected="{!boat.Id == v.selectedBoatId ? true : false }"/>
            </aura:iteration>
        </lightning:layoutItem>
    </lightning:layout>
</aura:component>

BoatSearchResultsController.js
({
    doInit : function(component, event, helper) {
        
    },
    doSearch : function(component, event, helper) {
        //debugger;
        var boatTypeId = component.get("v.boatTypeId");
        console.log('In boat search results 2 : ',boatTypeId);
        helper.onSearch(component, boatTypeId);
        console.log("Inside boat search results controller");
    },
    onBoatSelect : function(component, event, helper) {
        var boatId = event.getParam("boatId");
        console.log("onBoatSelect in BoatSearchResults.", boatId);
        component.set("v.selectedBoatId", boatId);
    }
})

BoatSearchResultsHelper.js
({
    onSearch : function(component, boatTypeId){
        var action = component.get( "c.getBoats" );
        var returnBoats;
        console.log("In helper of boat search results and value is ",boatTypeId);
        action.setParams({
            boatTypeId : boatTypeId
        });
        action.setCallback(this, function( response ){       
            //console.log(response.getReturnValue());
            returnBoats = response.getReturnValue()
            component.set("v.boats",returnBoats);
            //debugger;
            this.returnBoats = returnBoats;
            console.log("length is ",returnBoats.length);
        });
        $A.enqueueAction(action);
    }
})

returnBoats has value however, component.set("v.boats",returnBoats) statement is not setting any value to boats attribute as a result the aura component iteration is not executing tiles component.

Thanks,
Vinay
AbhinavAbhinav (Salesforce Developers) 
Hi Vinay,

Please note that Questions about how to pass Trailhead challenges are not on topic, because these challenges are intended to be independent demonstrations of your abilities.
Trailhead Help (https://trailhead.salesforce.com/en/help?support=home)can provide assistance for situations where Trailhead does not appear to be functioning correctly. You can reach out to them if this is the case.
Please close the thread by selected as best answer so that we can keep our community clean.

Thanks!
ravi soniravi soni
hi vinay,
try below js code and let me know if it helps you by marking it as best answer.
onSearch : function(component, boatTypeId){
        var action = component.get( "c.getBoats" );
       
        console.log("In helper of boat search results and value is ",boatTypeId);
        action.setParams({
            boatTypeId : boatTypeId
        });
        action.setCallback(this, function( response ){       
            //console.log(response.getReturnValue());//Make sure Result is coming here
             var returnBoats = response.getReturnValue();
            component.set("v.boats",returnBoats);
            //debugger;
           // this.returnBoats = returnBoats;
            console.log("length is ",returnBoats.length);
        });
        $A.enqueueAction(action);
    }
})

don't forget to mark it as best.
thank you​​​​​​​