Skip to main content
2Charlie
Inspiring
April 26, 2016
Answered

Element ARRAYROLES is undefined in a CFML structure referenced as part of an expression.

  • April 26, 2016
  • 1 reply
  • 1973 views

Here's my code:

<cfif StructKeyExists(cfData.data, "inherited_roles") || IsArray(cfData.data.reader_roles)>

  <cfset arrayRoles = "reader_roles" >

  <cfif ArrayIsEmpty(cfData.data.inherited_roles)>

  <cfset arrayRoles = "inherited_roles" >

  </cfif>

  <cfloop index="p" from="1" to="#arrayLen(cfData.data.arrayRoles)#">

     <!---some code here--->

  </cfloop>

</cfif>

And here's the error I kept getting.

Element ARRAYROLES is undefined in a CFML structure referenced as part of an expression.

The error occurred on line 07 when I have the #arrayLen(cfData.data.arrayRoles)#.

Any suggestion is much appreciated

    This topic has been closed for replies.
    Correct answer 2Charlie

    I even tried this <cfif #arrayLen(cfData.data.reader_roles)# lte 0 > and it error out as well.

    Error in custom script module


    Okay, I think I got it working now.

    <cfif not len(cfData.data.reader_roles)>

        <cfset arrayRoles = cfData.data.inherited_roles >

      <cfelse>

         <cfset arrayRoles = cfData.data.reader_roles >

      </cfif>

      <cfloop index="p" from="1" to="#arrayLen(arrayRoles)#">

      <!---code goes here--->

       </cfloop>

    Looks like the not len(cfData.data.reader_roles) do test if the array is empty or not. I'm not sure why the ArrayIsEmpty is not working in this case.

    1 reply

    EddieLotter
    Inspiring
    April 26, 2016

    The error is telling you the problem.

    Replace "arrayRoles" with "reader_roles" on line 7 and you won't get the error unless "cfData.data.reader_roles" is not an array, which could happen because of line 1.

    Perhaps you should explain what you are trying to achieve, perhaps there is a better way.

    Cheers

    Eddie

    2Charlie
    2CharlieAuthor
    Inspiring
    April 26, 2016

    First of, line 4 should be: <cfif ArrayIsEmpty(cfData.data.reader_roles)>

    Okay, by default, I am setting the ​cfData.data to reader_roles in line 4. However, if reader_roles array is empty, then set the arrayRoles variable to inherited_roles.

    So in cfloop line 7, I want to loop through only the array that actually have some data in there. Another way would be to do an if-else statement and put all the codes in there but I was thinking that by simply swapping the "reader_roles" for "inherited_roles" and vice versa at the end of cfData.data would work but ColdFusion throws an error.

    2Charlie
    2CharlieAuthor
    Inspiring
    April 27, 2016

    Here is a working example, modify to suite your needs:

    <cfscript>
      i = 1;

      cfData.data = arrayNew(1);
      cfData.data = structNew();

      cfData.data.inherited_roles = arrayNew(1);
      cfData.data.reader_roles = arrayNew(1);

      cfData.data.reader_roles[1] = 'reader role 1';
      cfData.data.reader_roles[2] = 'reader role 2';

      if ( isArray(cfData.data.inherited_roles) || IsArray(cfData.data.reader_roles) ) {

        if ( arrayIsEmpty(cfData.data.reader_roles) )
          arrayRoles = cfData.data.inherited_roles;
        else
          arrayRoles = cfData.data.reader_roles;

        for (nIndex=1; nIndex <= arrayLen(arrayRoles); nIndex++) {
          writeOutput(arrayRoles[nIndex]);
          writeOutput('<br>');
        }
      }
    </cfscript>

    Cheers

    Eddie


    I have a question on this line of code.

    ArrayIsEmpty(cfData.data.reader_roles)

    If I do IsArray(cfData.data.reader_roles) it returns true. It is in fact an array but when I do ArrayIsEmpty, it gave me the error below.

    Object of type class java.lang.String cannot be used as an array

    So, if it's an array, how do I test if it's an empty array or not in ColdFusion? I like to understand why it's giving me the error despite being an array.