简体   繁体   English

JavaScript控制台可以看到对象的内容,但是我无法访问它

[英]JavaScript console can see contents of Object but I can't access it

I'm trying to use the OpenLayers JavaScript library to load a number of KML files to a map. 我正在尝试使用OpenLayers JavaScript库将许多KML文件加载到地图上。 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. 我可以毫无问题地加载所有KML,但是现在尝试从正在生成的OpenLayers.Layer.Vector对象检索数据时,我遇到了问题。

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. 如果我用console.log输出生成的对象,我可以看到完整的对象,其所有属性都被充实了,但是如果我尝试以编程方式访问大多数属性,则会收到未定义的错误。 Here is my code: 这是我的代码:

    map = new OpenLayers.Map("mapdiv");
    map.addLayer(new OpenLayers.Layer.OSM());

    var vectors = new Array();

    for(i = 1; i <= 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...

So with just console.log(layer) I am able to see everything. 因此,仅使用console.log(layer)我就能看到所有内容。 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. 另外,当我尝试直接通过控制台访问相同的信息时,它也可以正常工作。

Am I missing something obvious here? 我在这里错过明显的东西吗?

EDIT: 编辑:

Here is the root Object 这是根对象

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

EDIT 2: 编辑2:

Won't be able to Stringify easily so here is some relevant information: 无法轻松进行Stringify,因此这里是一些相关信息:

inside "features": 内部“功能”:

0: Object
length: 1
__proto__: Array[0]
id: "OpenLayers.Layer.Vector_39"

inside "0": 在“ 0”内部:

attributes: Object
data: Object
geometry: Object
id: "OpenLayers.Feature.Vector_3508"
layer: Object
lonlat: null
renderIntent: "default"
state: null
style: null
inRange: true

inside "attributes": 在“属性”中:

name: <string, which I can assure you exists>

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. 为了澄清起见,当我在控制台中键入“ layer.features [0] .attributes.name”时,我尝试在其代码中使用的效果很好。

EDIT 3! 编辑3!

Tiers of output from direct access through console : 通过控制台直接访问的输出层

1) layer -> Object
2) layer.features -> Object
3) layer.features[0] -> Object
4) layer.features[0].attributes -> Object
5) layer.features[0].attributes.name -> <the string I'm looking for>
OR
6) layer.features[0].attributes['name'] works the same as above

Output from hardcoded script : 硬编码脚本的输出

1) layer -> Object
2) layer.features -> []
3) layer.features[0] -> undefined
etc... 

You should be able to see the properties provided you're following the path to them correctly. 您应该能够看到属性,前提是您正确地遵循了它们的路径。 Your full expression is layer.features[0].attributes.name . 您的完整表达式是layer.features[0].attributes.name What you've posted so far tells us that the layer.features and layer.features[0] parts are right, but we'd have to know what's in layer.features[0] to know whether it has an attributes property, etc. 到目前为止,您所发布的内容告诉我们layer.featureslayer.features[0]部分是正确的,但是我们必须知道layer.features[0]有什么内容才能知道它是否具有attributes属性,等等。 。

One thing that jumps out is that attributes is plural and then you're accessing a property on it directly. 跳出来的一件事是attributes是复数,然后您直接访问它的属性。 The plural suggests it may be an array, and so perhaps you need to look at a subscript? 复数形式表明它可能是一个数组,因此也许您需要查看下标?

For this stuff, I find nothing beats a debugger. 对于这些东西,我发现没有什么比调试器更好。 You can use one drill down into the layer object and find out exactly what's in there, and usually when you do that you even see the path to the point you've expanded to. 您可以使用一个向下钻取到layer对象的方法,并确切地找到其中的内容,通常在执行此操作时,您甚至会看到到扩展点的路径。 There are debuggers for every major browser out there: 每个主要浏览器都有调试器:

  • Chrome and Safari have "Dev Tools" built in (on Chrome Ctrl+Shift+I will open the panel) Chrome和Safari内置了“开发工具”(在Chrome上Ctrl + Shift +我将打开面板)
  • Firefox has the excellent Firebug add-in Firefox具有出色的Firebug加载项
  • IE has the free edition of Visual Studio.Net (and IE8 and above have some built-in tools as well) IE具有Visual Studio.Net的免费版本(并且IE8及更高版本也具有一些内置工具)

Edit : This part of your updated question is interesting: 编辑 :您的更新问题的这一部分很有趣:

2) layer.features -> [] 2) layer.features -> []
3) layer.features[0] -> undefined 3) layer.features[0] -> undefined

That suggests that either A) features is an empty array, or B) features is a sparse array that doesn't have an index 0 . 这表明要么A) features是一个空数组,要么B) features是一个没有索引0稀疏数组。 All JavaScript arrays are sparse (they're not really arrays at all), so you might try: 所有JavaScript数组都是稀疏的(它们根本不是数组),因此您可以尝试:

var name;
for (name in layer.features) {
    console.log(name + ": " + layer.features[name]);
}

You'll need to study the format of your object. 您需要研究对象的格式。 This is the long way around, but try this: 这是很长的路要走,但是尝试一下:

console.log(layer.features);
console.log(layer.features[0]);
console.log(layer.features[0].attributes);
console.log(layer.features[0].attributes.name);

Somewhere down the line you'll get an error, and that will tell you where your problem is. 在某个地方,您会得到一个错误,它将告诉您问题出在哪里。 I would guess, after seeing your current log, that either attributes or name does not exist (at least not in the format you think they do). 在看到您当前的日志后,我会猜测attributesname都不存在(至少不存在您认为的格式)。 (of course, it's better/easier to use the drill-down in firebug for this, but the snippet above will get you started.) (当然,为此,最好在Firebug中使用向下钻取,但上面的代码片段将帮助您入门。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使可以看到对象的内容,也无法访问其属性 - I can't access the properties of an object even though I can see it's contents 无法在 javascript 中访问子对象,但我看到了 - Can't access child object in javascript, nevertheless I see it mootools类无法访问对象,但可以在控制台中看到它 - mootools class cannot access object but I can see it in the console 我可以在控制台中看到对象的字段变量,但无法访问它 - I can see the object's field variable in the console but cannot access it 无法使用 javascript 访问 SVG 内容 - Can't access SVG contents with javascript JavaScript无法看到对象ID - Javascript can't see an object id 如何使用节点调试器查看JavaScript Map的内容? - How can I see the contents of a JavaScript Map using the node debugger? nodejs:Error.captureStackTrace() 添加堆栈,我可以访问它,但是当我控制台记录错误时我看不到它 - nodejs: Error.captureStackTrace() adds stack, and I can access it, but I can't see it when I console log the error Visual Studio 2015:在JavaScript控制台中看不到对象内容 - Visual studio 2015: can't see objects content in javascript console 在JS Console Chrome中看不到对象的原型 - Can't see prototype of object in JS Console Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM