1

I have yet another Locker Service question. I have built a number of Lightning components over the last few months, and they all work great, but when I enabled Locker Service I noticed that places where I use a For Loop, the components fail.

The following has a Map<String, String> returned, and it works great when Locker Service is not enabled.

 doInit : function(component, event, helper) {
    var action = component.get("c.findAll"); 
    var theAmens = component.get("v.AmenityList");

    action.setParams({ 'amenityTypes' : theAmens });
    action.setCallback(this, function(response) {
        var state = response.getState();
        if (state === "SUCCESS") {                
            var spcs = [];
            var theSpaces = response.getReturnValue();
            console.log("the Spaces " + theSpaces);
            for ( key in theSpaces ) {
                spcs.push({value:theSpaces[key], key:key});
            }
            component.set("v.spaces", spcs);
        }  
  });           
  $A.enqueueAction(action);
} 

Has anyone else had a similar problem, and what is the solution?

3

From my observations, Locker Service adds getter/setter functions to your objects (according to what level of access you should have to them i.e. no setter if you shouldn't be able to write the field). So, the problem in your code might be that you're picking up those new functions in your for loop.

You could try this:

for (var key in theSpaces ) {
  var value = theSpaces[key];
  if(typeof value != 'function') {
    spcs.push({value:value, key:key});
  }
}

If that's not it, could you be more specific about any error messages that you're getting?

  • Thanks Aidan, I tried that code and got the following error: Uncaught Error in $A.getCallback() [key is not defined] Callback failed: apex://SpaceListControllerApex/ACTION$findAll – Logan Jun 8 '17 at 15:16
  • @Logan Ooops, that was copied from your code, but I didn't account for the fact that Locker Service works in strict mode, so I needed to include "var key" in the for loop. I've edited my code, so try again... – Aidan Jun 8 '17 at 15:19

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.