Atlaslog / Field Notes / OpenLayers layers
Field note · OpenLayers layers
A style function missing return drops most features
Quick answer
The style function handles the alarm branch and falls through without returning for the normal branch, so most features get undefined. With declutter enabled the renderer throws while calling getType on that value.
Symptom
- Only the 120 features with status 'alarm' render; the other 680 disappear from the map.
- Uncaught TypeError: Cannot read properties of undefined (reading 'getType') at executeDeclutter (VectorLayer.js:1268).
- layer.getStyleFunction()(feature, resolution) prints undefined for a normal feature.
Checklist
- Call layer.getStyleFunction() on a few features and print the returned value.
- Check every branch of the style function for an explicit return.
- Check whether null is used where not-rendered is intended.
- Check whether declutter is enabled, since it turns undefined into a thrown error.
Fix
Return an ol.style.Style for each branch and return null explicitly for hidden features, pre-building the style objects once outside the callback instead of creating them per feature.
Code
Uncaught TypeError: Cannot read properties of undefined (reading 'getType')
at executeDeclutter (VectorLayer.js:1268)var styleFn = layer.getStyleFunction();
features.slice(0, 5).forEach(function (f) {
var s = styleFn(f, map.getView().getResolution());
console.log(f.get('status'), s && s.length, s);
});
// alarm 1
// normal undefined ← 问题所在
Watch out
- Returning undefined instead of null looks the same in source but crashes declutter and skips rendering.
- Constructing new ol.style.Style inside the function for 800 features each frame causes GC pressure and slow redraws.
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Atlaslog (2026). A style function missing return drops most features. https://www.jingweizhi.com/en/cookbook/ol-style-function-undefined.html (accessed 2026-11-03).