Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript console can see contents of Object but I can't access it
    text
    copied!<p>I'm trying to use the OpenLayers JavaScript library to load a number of KML files to a map. I can load all of the KML without a problem, but now in trying to retrieve data from the OpenLayers.Layer.Vector object being generated I'm running in to an issue.</p> <p>If I output the generated object with console.log I can see the full object with all of it's properties fleshed out, but if I try to access most of the properties programatically I'm getting an undefined error. Here is my code:</p> <pre><code> map = new OpenLayers.Map("mapdiv"); map.addLayer(new OpenLayers.Layer.OSM()); var vectors = new Array(); for(i = 1; i &lt;= 14; i++ ) { var layer = new OpenLayers.Layer.Vector("KML", { strategies: [new OpenLayers.Strategy.Fixed()], protocol: new OpenLayers.Protocol.HTTP({ url: "kml/" + i + ".kml", format: new OpenLayers.Format.KML }) }); console.log(layer); console.log(layer.features[0].attributes.name); vectors.push(layer); } //etc... </code></pre> <p>So with just console.log(layer) I am able to see everything. With the following line, though, although the information clearly exists, I get the error. Also, when I try to access the same information directly through the console it works just fine.</p> <p>Am I missing something obvious here?</p> <p>EDIT: </p> <p>Here is the root Object</p> <pre><code>Object: EVENT_TYPES: Array[25] alwaysInRange: true div: HTMLDivElement drawn: true events: Object features: Array[1] id: "OpenLayers.Layer.Vector_39" inRange: true map: Object maxExtent: Object maxResolution: 1.40625 maxScale: 13517.573318481445 minExtent: null minResolution: 0.00004291534423828125 minScale: 442943842.5 name: "KML" numZoomLevels: 16 options: Object projection: Object protocol: Object renderer: Object resolutions: Array[16] scales: Array[16] selectedFeatures: Array[0] strategies: Array[1] styleMap: Object tileSize: Object units: "degrees" unrenderedFeatures: Object __proto__: Object </code></pre> <p>EDIT 2:</p> <p>Won't be able to Stringify easily so here is some relevant information:</p> <p>inside "features":</p> <pre><code>0: Object length: 1 __proto__: Array[0] id: "OpenLayers.Layer.Vector_39" </code></pre> <p>inside "0":</p> <pre><code>attributes: Object data: Object geometry: Object id: "OpenLayers.Feature.Vector_3508" layer: Object lonlat: null renderIntent: "default" state: null style: null inRange: true </code></pre> <p>inside "attributes":</p> <pre><code>name: &lt;string, which I can assure you exists&gt; </code></pre> <p>For clarification the "layer.features[0].attributes.name" that I am attempting to use in my code works perfectly fine when I type it in to the console.</p> <p>EDIT 3!</p> <p><strong>Tiers of output from direct access through console</strong>:</p> <pre><code>1) layer -&gt; Object 2) layer.features -&gt; Object 3) layer.features[0] -&gt; Object 4) layer.features[0].attributes -&gt; Object 5) layer.features[0].attributes.name -&gt; &lt;the string I'm looking for&gt; OR 6) layer.features[0].attributes['name'] works the same as above </code></pre> <p><strong>Output from hardcoded script</strong>:</p> <pre><code>1) layer -&gt; Object 2) layer.features -&gt; [] 3) layer.features[0] -&gt; undefined etc... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload