开发者

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

开发者 https://www.devze.com 2023-01-10 15:17 出处:网络
In ColdFusion, when I call IsDefined(\"root.L1[1].L2\"), I am getting the following error: Parameter 1 of function IsDefined, which is now root.L1[1].L2, must be a syntactically valid variable name.

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

Parameter 1 of function IsDefined, 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!
    WriteOu开发者_StackOverflow中文版tput("It is defined!");
  else
    WriteOutput("It is not defined!");
</cfscript>


Try

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

instead of isDefined()

I vaguely recall there being issues with complex variables with isdefined(), but I can't recall the version.


As was mentioned in a follow-up comment you should stack the logic checks a la:

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

The parser will skip the second logical argument if the first fails, so you won't get an error on the second if the first passes.

0

精彩评论

暂无评论...
验证码 换一张
取 消