ColdFusion error using IsDefined (): parameter must be a syntactically valid variable name

In ColdFusion, when I call IsDefined("root.L1[1].L2"), I get the following error:

Parameter 1 of the IsDefined function, which is now root.L1 [1] .L2, must be a syntactically valid variable name.

This is a valid variable name, so what gives?

Here is my simplified test code:

<cfscript>
  root = StructNew();
  root.L1 = ArrayNew(1);
  root.L1[1] = StructNew();
  root.L1[1].L2 = "foo";

  WriteOutput("root.L1[1].L2 is: #root.L1[1].L2#<br/>"); //no exception

  if(IsDefined("root.L1[1].L2")) //exception!
    WriteOutput("It is defined!");
  else
    WriteOutput("It is not defined!");
</cfscript>
+3
source share
2 answers

Try

StructKeyExists(root.L1[1],"L2")

instead of isDefined ()

I vaguely remember that there were problems with complex variables with isdefined (), but I can’t remember the version.

+12
source

As mentioned in a subsequent comment, you should add logical checks a la:

    if(arrayLen(root.L1) gte 1 AND structKeyExists(root.L1.[1],'L2')){ }

, , , .

0

Source: https://habr.com/ru/post/1757904/


All Articles