I try to pass a map from my lwc to my Apex controller after when i click in button
every time I get null
Attempt to de-reference a null object
Do you have suggestions ? where i was wrong
if I want to put on a simple String it works.
there is data in the map check with console.log on the javascript side
Controller Apex
@AuraEnabled(cacheable=true)
public static String getresults(Map<String, String> myMap){
System.debug('myMap '+ myMap) ;
}
LWC Javascript
I tried like that
import { LightningElement,wire,api,track } from 'lwc';
import getresult from '@salesforce/apex/Controller.getresults';
export default class Ecranlwc extends LightningElement {
**@track myMap = [];**
buttonClick(event)
{
var inp=this.template.querySelectorAll("lightning-input");
inp.forEach(function(element){
if(element.name ==="name"){
**this.myMap.push({key:"name",value:element.value});**
}
}
console.log('myMap ' + this.myMap) ;
getresults({ myMap: myMap })
.then(result => {
console.log('result '+JSON.stringify(result));
})
.catch(error => {
console.log('-------error -------------'+ JSON.stringify(error));
console.log(error);
});
}
}
And like that
import { LightningElement,wire,api,track } from 'lwc';
import getresult from '@salesforce/apex/Controller.getresults';
export default class EcranLWC extends LightningElement {
buttonClick(event)
{
var inp=this.template.querySelectorAll("lightning-input");
**let myMap = new Map()**
inp.forEach(function(element){
if(element.name ==="name"){
**myMap.set('name',element.value);**
}
}
console.log('myMap ' + myMap) ;
getresults({ myMap: myMap })
.then(result => {
console.log('result '+JSON.stringify(result));
})
.catch(error => {
console.log('-------error -------------'+ JSON.stringify(error));
console.log(error);
});
}
}
Best Answer
There's a few things that I can see. For starters your first example has "map" defined as an array:
Your second uses the JS Map object which will not map to anything in apex.
Apex Data structures
A customer can be represented as
Class
Map
JS Maps
The JS Map object will not translate to any native objects in SF. I suppose in theory you could create a class in SF that might be able to work but thats much more work then necessary.
A
map
in Apex more closely translates to anObject
in javascript. The Key in the map is the Name of the attribute in the Object and the value in the map is the value of the attribute. Lets use the below JS as an example:Passing data to apex
There are 2 ways to pass data like this to apex. First, is to pass it into an appropriately typed parameter
The second is to JSONify your data and pass it as a string
Javascript
Apex
With aura components I believe that the second option was a more reliable approach as the built in serialization/deserialization was known to cause issues with some data types, including custom classes (I believe a custom class needs to be a top level class to work as a parameter but with this approach it can be a second level class).
Conclusion and TL;DR
The most reliable approach is to always pass your data as a JSON string and handle the deserialization on the apex side yourself. This does require you to know how to properly deserialize json in Apex.
Additional notes
JSON based REST services operate the same way. See this answer which I think tries to convey the same information but in the context of JSON REST services.