• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Enthusiast ,
Apr 26, 2016 Apr 26, 2016

Copy link to clipboard

Copied

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

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Apr 27, 2016 Apr 27, 2016

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.

Votes

Translate

Translate
Advocate ,
Apr 26, 2016 Apr 26, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 26, 2016 Apr 26, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 26, 2016 Apr 26, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 26, 2016 Apr 26, 2016

Copy link to clipboard

Copied

This code:

<cfif ArrayIsEmpty(cfData.data.reader_roles)>

    <cfset arrayRoles = cfData.data.inherited_roles >

  <cfelse>

    <cfset arrayRoles = cfData.data.reader_roles >

  </cfif>

Causes this error:

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 26, 2016 Apr 26, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 27, 2016 Apr 27, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 27, 2016 Apr 27, 2016

Copy link to clipboard

Copied

The following snippet works as expected.

testArray = arrayNew(1);

if ( isArray(testArray) ) {

  writeOutput('testArray is an array.<br>');

  if ( arrayIsEmpty(testArray) )

    writeOutput('testArray is empty.<br>');

  else

    writeOutput('testArray is not empty.<br>');

}

Can you provide a similar snippet that demonstrates the failure you are talking about?

Cheers

Eddie

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 27, 2016 Apr 27, 2016

Copy link to clipboard

Copied

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

Error in custom script module

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 27, 2016 Apr 27, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 27, 2016 Apr 27, 2016

Copy link to clipboard

Copied

As I said, please show a reproducible snippet of the failure, unless you feel this is no longer an issue, in which case please close this thread. It is best for the community to keep each thread focused on a single issue.

Cheers

Eddie

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 27, 2016 Apr 27, 2016

Copy link to clipboard

Copied

LATEST

It's no longer an issue. Looks like not len() works. I was just not sure why ArrayIsEmpty and arrayLen() is not working for testing if the array is empty.

Many thanks for your help though. I'd appreciated.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation