0

I've been using E4X expressions and it has never caused a problem. Today I got this error which is driving me crazy. Checked everything thousand times, searched web, but nothing.

So this is my query for the value:

var objectName:String = myXML.objects.object.(@id==objectId);

where objectId is an int.

This is the part of my XML:

<objects>

    <object id="0">value 1</object>
    <object id="1">value 2</object>
    <object id="2">value 3</object>

</objects>

When the above line is run I get an error which seems like a total nonsense to me:

ReferenceError: Error #1065: Variable id is not defined.
Gio
  • 1,954
  • 3
  • 22
  • 42

1 Answers1

1

It's because not all of your object nodes has the id attribute, try more safety way to search through the attributes with hasOwnProperty("@id") checks:

    var objectName:String = 
        xml.objects.object.(hasOwnProperty("@id") && @id=="2");
fsbmain
  • 5,267
  • 2
  • 16
  • 23
  • Actually all of my "object" nodes have an attribute `id`, but thanks for your approach. – Gio Aug 28 '13 at 11:58
  • Now I'm getting a "Value is not a function" error message, as I also manipulate the string I get from XML. I think something's wrong with either my XML or my code. Going to debug further. Thanks – Gio Aug 28 '13 at 12:06
  • After debugging my XML I found out that somehow `xml.objects` only contains first `object` node, which doesn't have an `id` listed when traced. Any opinions? – Gio Aug 28 '13 at 12:26
  • After further debugging I found out my code was messing things up, when I tried to replace all the `\\n` characters with `\n`s. Strange.. – Gio Aug 28 '13 at 12:31