You need to sign in to do that
Don't have an account?
ScottB3
Trouble returning a Map from the lightning component controller to helper.js
I have a component that is calling a controller method which then returns a Map of object Ids and the object name back to the component.
The Map is successfully created in the controller as I can see the values in the debug logs.
However, when I call response.getReturnValue(), it just says that the value is [object Object] and I can't do anything with it. I see no values, the response.getReturnValue().length is 0.
If I return a string instead of a map, it works as would be expected.
Component
Controller
Helper
The Map is successfully created in the controller as I can see the values in the debug logs.
However, when I call response.getReturnValue(), it just says that the value is [object Object] and I can't do anything with it. I see no values, the response.getReturnValue().length is 0.
If I return a string instead of a map, it works as would be expected.
Component
<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global" controller="WFS_ObjectsIFollowController" > <aura:attribute name="recordId" type="String" /> <aura:attribute name="targetFields" type="Object"/> <aura:attribute name="recordError" type="String"/> <aura:attribute name="followedObjectsCount" type="Integer" default="0"/> <aura:attribute name="parentObjects" type="Map"/> <force:recordData aura:id="recordLoader" recordId="{!v.recordId}" layoutType="FULL" targetFields="{!v.targetFields}" targetError="{!v.recordError}" recordUpdated="{!c.recordUpdated}" fields="Id" mode="VIEW"/> <article class="slds-card "> <div class="slds-card__body slds-card__body_inner"> <aura:if isTrue="true"> <div class="slds-region_narrow"> Size = {! v.followedObjectsCount } </aura:if> </div> </div> </article> </aura:component>
Controller
({ recordUpdated : function(component, event, helper) { var changeType = event.getParams().changeType; if (changeType === "ERROR") { /* handle error; do this first! */ console.log("Error: ");} else if (changeType === "LOADED") { /* handle record load */ console.log("*** In the controller"); helper.getData(component, event, changeType) } else if (changeType === "REMOVED") { /* handle record removal */ console.log("Removed: ");} else if (changeType === "CHANGED") { /* handle record change */ helper.getData(component, event, changeType) } } })
Helper
({ getData : function(component, event, cType) { console.log("In the WFS_Objects_I_FollowHelper.js function"); var action = component.get('c.getObjectsIFollow'); action.setCallback(this, function(response) { var state = response.getState(); if(state === "SUCCESS"){ alert(response.getReturnValue()); component.set('v.parentObjects', response.getReturnValue()); component.set('v.followedObjectsCount', response.getReturnValue().length); console.log("Objects Returned: " + response.getReturnValue().length); }else { console.log("RESPONSE FAILURE"); } }); $A.enqueueAction(action); } })Apex Controller
public class WFS_ObjectsIFollowController { @AuraEnabled public static Map<String, String> getObjectsIFollow(){ List<Case> followedCases; Map<String, String> parentObjects = new Map<String, String>(); String userId = UserInfo.getUserId(); String soql = 'SELECT Id, Subject FROM Case WHERE Id IN (SELECT ParentId FROM EntitySubscription WHERE SubscriberId = :userId)'; followedCases = Database.query( soql ); for ( Case parent : followedCases ){ String parentId = parent.Id; String caseSubject = parent.Subject; parentObjects.put(parentId, caseSubject); System.debug('*** OBJECTS I FOLLOW - ' + parentId + ' - ' + caseSubject); } System.debug('Parent Objects --- ' + parentObjects); //THIS LINE PRINTS WITH THE CORRECT INFO return parentObjects; } }
alert(response.getReturnValue()); this gives [object object] irrespective of the length. I see no issues with the code above. Can you add debugger and debug the js file.
Add debugger; above this line alert(response.getReturnValue()); and see if you can see the map records in the browser. Open the browser developer console before reproducing.
Naveen
What I now cannot seem to figure out is how to get the size of that map as is attempted in lines 13, 14
I also do not seem to have any value when I assign it to the aura:attribute on line 12.