Atlaslog
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.

Last verified 2026-11-03 · Multiple versions · Cite this page
Applies to: OpenLayers
Symptom
Checklist
  1. Call layer.getStyleFunction() on a few features and print the returned value.
  2. Check every branch of the style function for an explicit return.
  3. Check whether null is used where not-rendered is intended.
  4. 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
All 120 field notesChinese originalRunnable examples
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Cite this page

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).