0

In the javascript controller of my lightning component, I need to call an apex method and pass it a map as parameter.

I have issues when setting the value in the map, in the javascript.

Now I have something like this in the javascript controller :

handleGetResult : function(component, event, helper) {
    var lines = component.get('v.listLines');
    console.log('lines : ');
    console.log(lines);

    //send a map as a parameter of the apex method
    var mapLines = {};
    for(var key in lines.keys()){
         mapLines[key] = lines.get(key);
    }

    console.log('mapLines : ');
    console.log(mapLines);

    //call apex method that construct the query
    var action = component.get('c.manageFilters');
    action.setParams({
        lines : mapLines
    });
    action.setCallback(this, function(response){
        var state = response.getState();
        if (state === "SUCCESS"){

        }
    });
    $A.enqueueAction(action);
}

But here mapLines is null even if I put many item in it via the lightning component. lines is not null and contains all the items.

I don't know how to create the map in javascript.

The apex method so far :

@AuraEnabled
public static String manageFilters(Map<String, Object> lines){
    System.debug('### lines : ' + lines);

    return null;
}
1

You may do it this way:

var lines = component.get('v.listLines');
console.log('lines : ', lines);

//send a map as a parameter of the apex method
var mapLines = {};
for(var i=0; i<lines.length;i++ ){
    mapLines[i] = lines[i];
}

console.log('mapLines : ', mapLines);

Below is the log output that I got from my test: enter image description here

1

Try this code ,

 var mapToSend = {}
  for (var key of lines.keys()) {
      mapToSend[key] = lines.get(key);
  }
  console.log('map tosend:::', mapToSend);
  action.setParams({
      lines: mapToSend
  });

Apex class:

 @AuraEnabled
 public static void manageFilters(Map<String, Object> lines ){
System.debug(JSON.serialize(lines )); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.