How to send Javascript map as a parameter from lightning component to Apex server controller?
Don't use Map, just use a normal Object. Here's a demonstration:
Apex:
public class LightningMap {
@AuraEnabled public static void doMap(Map<String, Object> values) {
System.debug(values);
}
}
Lightning Controller:
({
doInit: function(component, event, helper) {
var action = component.get("c.doMap");
action.setParams(
{
"values": {
"Hello": 5,
"World": 15
}
}
);
action.setCallback(this, function(result) {} );
$A.enqueueAction(action);
}
})
Demo App:
<aura:application controller="LightningMap">
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
</aura:application>
Logged output:
13:16:24.0 (1462121)|USER_DEBUG|[3]|DEBUG|{Hello=5, World=15}
For a more directly applicable version, this is probably what you want:
var mapToSend = {}
for (var key of valueMap.keys()) {
mapToSend[key] = valueMap.get(key);
}
action.setParams({
"name": cmp.get("v.name"),
"params": mapToSend
});
UPDATE
Ok, I was bugged by this one and made my own test app.
The result is: you CAN have a Map
. You just need to make sure it's declared
in the markup and has a default="{}"
attribute specified (I initially specified some keys, but you don't even need those, just empty braces)
So, component markup:
<aura:component controller="Your_Controller">
<aura:attribute name="theMap" type="Map" default="{}"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
</aura:component>
Controller code:
({
doInit : function(component, event, helper) {
var action = component.get("c.testMap");
var theMap = component.get("v.theMap");
//add some params
theMap["key1"]="blah1";
theMap["key2"]="blah2";
action.setParams({
"params": theMap
});
action.setCallback(this, function(result) {} );
$A.enqueueAction(action);
}
})
Server side controller:
@AuraEnabled
public static void testMap(Map<String, Object> params){
System.debug(JSON.serialize(params));
}
So you CAN have a Map. You just need to declare it in the markup! Hooray!!