0

({

jsLoaded: function(component, event, helper) {
   var accounts = event.getParam('accounts');
    for (var i=0; i<accounts.length; i++) {          
        var account = accounts[i];
        var map = L.map('map', {zoomControl: false}).setView([account.BillingLatitude, account.BillingLongitude],11);
        L.tileLayer(
        'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', {
        }).addTo(map);
    component.set("v.map", map);

    var circle = L.circle([account.BillingLatitude, account.BillingLongitude], {
        color: 'red',
        fillColor: '#f03',
        fillOpacity: 0.5,
        radius:10000
    }).addTo(map);

    L.marker([account.BillingLatitude, account.BillingLongitude]).addTo(map).bindPopup(account.Name).openPopup(); 
    //L.marker([account.BillingLatitude, account.BillingLongitude]).addTo(map).bindPopup('Hyatt').openPopup(); 
    //L.marker([account.BillingLatitude, account.BillingLongitude]).addTo(map).bindPopup('Marriott Marquis').openPopup();


    }      
}

})

1 Answer 1

1

Add an empty array check before getting length of the array. The Array.isArray() method determines whether the passed value is an Array.

var accounts = event.getParam('accounts');
if(Array.isArray(accounts)){
   for (var i=0; i<accounts.length; i++) {     
   }
}

Check it out:-

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray

2
  • +1 although, that's really not necessary, you can just write accounts && accounts.forEach(account => { ... });
    – sfdcfox
    Commented Apr 23, 2020 at 12:51
  • After adding accounts && accounts.forEach(account => { ... }); the error goes way but the screen goes blank it doesn't populate any map and markers Commented Apr 23, 2020 at 13:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .