You need to sign in to do that
Don't have an account?
Athira Venugopal
Issue in server side Apex controller
I am working on Lightning aura components. The problem is in my servide side apex controller, its not returning the expected result. Te result is [Object,Object]
BoatSearchResults.cmp
<aura:component controller = "BoatSearchResults" implements="flexipage:availableForAllPageTypes" access="global" >
<aura:attribute name="boats" type="Boat__c[]"/>
<lightning:layout multipleRows="true">
<lightning:button variant="brand" label="Search" onclick="{! c.searchBoat }" aura:id="SearchButton" />
<aura:if isTrue="{!v.boats.length > 0}">
<aura:iteration items="{!v.boats}" var="bot">
<lightning:layoutItem size="3" flexibility="grow" class="slds-m-around_small">
<c:BoatTile boat="{!bot}" />
</lightning:layoutItem>
</aura:iteration>
<aura:set attribute="else">
<lightning:layoutItem class="slds-align_absolute-center" flexibility="auto" padding="around-small">
<ui:outputText value="No boats found" />
</lightning:layoutItem>
</aura:set>
</aura:if>
</lightning:layout>
</aura:component>
js file
({
searchBoat : function(component, event) {
let action = component.get("c.getBoats");
action.setCallback(this, function(response){
let state = response.getState();
if (state === "SUCCESS") {
console.log('TEST ' + response.getReturnValue());// this ouput is [Object, Object]
component.set("v.boats", response.getReturnValue());
}
});
$A.enqueueAction(action);
},
})
BoatSearchResults.apxc
public with sharing class BoatSearchResults {
@AuraEnabled
public static List<Boat__c> getBoats() {
return [select id,Name,BoatType__c,Contact__c,Picture__c from Boat__c];
}
}
BoatTile.cmp
<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="boat" type="Boat__c"/>
<lightning:button class="tile">
<!-- Image -->
<div style="{!'background-image: url(\'' + v.boat.Picture__c + '\')'}" class="innertile">
<div class="lower-third">
<h1 class="slds-truncate">{!v.boat.Name}</h1>
</div>
</div>
</lightning:button>
</aura:component>
BoatSearchResults.cmp
<aura:component controller = "BoatSearchResults" implements="flexipage:availableForAllPageTypes" access="global" >
<aura:attribute name="boats" type="Boat__c[]"/>
<lightning:layout multipleRows="true">
<lightning:button variant="brand" label="Search" onclick="{! c.searchBoat }" aura:id="SearchButton" />
<aura:if isTrue="{!v.boats.length > 0}">
<aura:iteration items="{!v.boats}" var="bot">
<lightning:layoutItem size="3" flexibility="grow" class="slds-m-around_small">
<c:BoatTile boat="{!bot}" />
</lightning:layoutItem>
</aura:iteration>
<aura:set attribute="else">
<lightning:layoutItem class="slds-align_absolute-center" flexibility="auto" padding="around-small">
<ui:outputText value="No boats found" />
</lightning:layoutItem>
</aura:set>
</aura:if>
</lightning:layout>
</aura:component>
js file
({
searchBoat : function(component, event) {
let action = component.get("c.getBoats");
action.setCallback(this, function(response){
let state = response.getState();
if (state === "SUCCESS") {
console.log('TEST ' + response.getReturnValue());// this ouput is [Object, Object]
component.set("v.boats", response.getReturnValue());
}
});
$A.enqueueAction(action);
},
})
BoatSearchResults.apxc
public with sharing class BoatSearchResults {
@AuraEnabled
public static List<Boat__c> getBoats() {
return [select id,Name,BoatType__c,Contact__c,Picture__c from Boat__c];
}
}
BoatTile.cmp
<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="boat" type="Boat__c"/>
<lightning:button class="tile">
<!-- Image -->
<div style="{!'background-image: url(\'' + v.boat.Picture__c + '\')'}" class="innertile">
<div class="lower-third">
<h1 class="slds-truncate">{!v.boat.Name}</h1>
</div>
</div>
</lightning:button>
</aura:component>
Try
console.log(JSON.stringify(response.getReturnValue())); to check the result you are getting
This is not the issue with the server-side apex controller.
console.log('TEST ' + response.getReturnValue());// this ouput is [Object, Object]
Here you are trying to concatenate object and string.
To show output, simply stringify the return value like:
console.log('TEST'+JSON.stringify(response.getReturnValue()));
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Sachin Arora
www.sachinsf.com