It seems like collection types returned from the Apex to an Aura component omit null elements.
I noticed this when I was passing a List of Strings back from Apex. I was failing to get the second element of the List, despite having set the second element manually. Turns out the first element was not making it back to the Aura. I switched to a Map, which fixed the issue, but even Map elements with null values are being omitted.
Component:
<aura:component access="GLOBAL" controller="NullResponse_Apex" implements="lightning:isUrlAddressable">
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
</aura:component>
doInit: function(component, event, helper) {
helper.getList(component, event, helper);
helper.getMap(component, event, helper);
}
getList: function(component, event, helper) {
var action = component.get('c.returnListWithNull');
action.setCallback(this, function(result){
var status = result.getState();
if (status==='SUCCESS') {
console.log('List: ', result.getReturnValue());
}
});
$A.enqueueAction(action);
},
getMap: function(component, event, helper) {
var action = component.get('c.returnMapWithNull');
action.setCallback(this, function(result){
var status = result.getState();
if (status==='SUCCESS') {
console.log('Map: ', result.getReturnValue());
}
});
$A.enqueueAction(action);
}
Apex:
@AuraEnabled
public static List<String> returnListWithNull() {
List<String> strList = new List<String>();
strList.add(NULL);
strList.add('Test');
return strList;
}
@AuraEnabled
public static Map<String,String> returnMapWithNull() {
Map<String,String> strMap = new Map<String,String>();
strMap.put('a',NULL);
strMap.put('b','Test');
return strMap;
}
Response in console:
List: ["Test"]
Map: {b: "Test"}
Is this expected behavior? Has anyone else run into this issue?