I have about 1000 polygons I'm load onto a map and I'm trying to color them based on a properties value. I have the code below which produces the following results.
mapLayerFieldGrid = new L.GeoJSON.AJAX("/../static/data/fields/field001.geojson", {style: styleFieldGrid});
mapLayerFieldGrid.eachLayer(function(layer) {
    if(layer.feature.properties.health == 1) {
      layer.setStyle({fillColor: plantHealth1});
    } else if(layer.feature.properties.health == 2) {
      layer.setStyle({fillColor: plantHealth2});
    } else if(layer.feature.properties.health == 3) {
      layer.setStyle({fillColor: plantHealth3});
    } else if(layer.feature.properties.health == 4) {
      layer.setStyle({fillColor: plantHealth4});
    } else if(layer.feature.properties.health == 5) {
    layer.setStyle({fillColor: plantHealth5});
    }
});
mapLayerFieldGrid.addTo(mapField);
The polygons are there but they aren't colored correctly. When I run the eachLayer function in the console I do get the right result, which looks like this.
So my question is why does this work in the console, but not when the page loads, and how do I correct this? Any help is greatly appreciated.


 
    