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.

    EddieLotter
    Inspiring
    April 26, 2016

    You're assigning a string value to arrayRoles and then trying to use it as an array.

    Try the following instead (assuming both are indeed arrays):

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

      <cfif ArrayIsEmpty(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)#"> 
        <!---some code here---> 
      </cfloop>

    </cfif>

    Cheers

    Eddie