I'm using OpenLayers 7.5.2 to create a simple map with one vector layer having two points. This works. I want to then access the layer object in the map and get to the point features to update their text attributes. However, I get "Uncaught TypeError: l.getSource is not a function" in the browser when I attempt to access the style objects, starting with getSource():

    let lyrs = map_obj.getAllLayers(); //works
    for (let l in lyrs) {
        source = l.getSource(); //doesn't work
        features = source.getFeaturesCollection(); //??
        for (let f in features) {
            alert('a feature');                 

Per the ol documentation, the getSource method belongs to the layer object. This seems straight-forward so I'm at a loss. I'm new to OpenLayers.

Here's my test page for reference.

Can you give me any pointers?

  • 2
    for ... in works on objects, not arrays, although lyrs[l].getSource() would work as it treats the array index as an object key.
    – Mike
    Sep 3, 2023 at 13:21
  • @Mike - Ah, right! Thanks for the reminder. Sep 3, 2023 at 13:52
  • If that has solved the problem, then post it as an answer to your own question (and accept the answer as correct). Then everyone can see that this question is resolved. Sep 5, 2023 at 2:51

1 Answer 1


In in my code sample, above, the syntax of l.getSource() is incorrect. Here's an updated code sample with corrected syntax.

let lyrs = map_obj.getAllLayers(); 
for (let l in lyrs) {
    source = lyrs[l].getSource();           
    let features = source.getFeaturesCollection();          
    features.forEach( function (feat, index) {  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.