Arcade Expression Cannot read property 'length' of undefined error

2834
14
Jump to solution
10-28-2020 05:59 AM
PaulSweeney3
Occasional Contributor III

Hi All 

See Acade expression 

Var polesLayer =  FeatureSetByPortalItem(Portal('myportal'), 'xxxxxxxxxx')
var chambers  = FeatureSetByPortalItem(Portal(''my portal'), 'xxxxxxxxxx')
Var buff = buffer($feature,1,'meters')
Var poles = Intersects(buff,polesLayer)
Var chambers  = Intersects(buff,chambers)
var cnt = count(poles)
var cntchambers = count(chambers) 
var popupResult = ''
for (var f in poles) {

popupResult += f.barcode + " to pole " 

}var ChambResult = ''
for (var f in chambers) {

ChambResult += f.label + " to chamber " 

}
var strLenght = count(popupResult)-8
var note = left(popupResult,strLenght)
var strLenght2 = count(ChambResult)-15
var note2 = left(ChambResult,strLenght2)
var popup = IIf(cnt==1, replace(("install subduct " + DomainName($feature,"sub_size")+ " from pole " +text(note)),'to pole',''), ("install subduct " + DomainName($feature,"sub_size")+ " from pole " +text(note)))
var popup2 = IIf(cntchambers==1, replace(("install subduct " + DomainName($feature,"sub_size")+ " from chamber " +text(note2)),'to chamber',''), ("install subduct " + DomainName($feature,"sub_size")+ " from chamber " +text(note2)))
var popup3 = "install subduct " + DomainName($feature,"sub_size")+" from the chamber "+ChambResult+" to the pole "+popupResult
var popup3 = replace(popup3,' to pole ',"")
var popup3 = replace(popup3,' to chamber ',"")
var finalresult = when(cnt <= 2 && cntchambers==0,popup,cnt == 0 && cntchambers==2, popup2,cnt == 1 && cntchambers==1, popup3,'n/a')
return finalresult‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 i am using this expression to check if a line is intersecting two point feature layers and return an expression to run a specific type of subduct between each feature. the expression works as expected in the pop up but when i try to run it in the field calculator it fails with the expression 

"Cannot read property 'length' of undefined error"

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi Paul Sweeney ,

Sorry for the late reply but I was attending the IMGIS conference and yesterday was a national holiday.

I just changed your code slightly, but it is still not very convincing. A couple of comments:

  • In your code do you have the layerID when you use FeatureSetByPortalItem for poles and chambers?
  • The validation of the count is before you do the loop for both poles and chambers
  • I am not convinced with the final When, since there are a number of cases that in theory can occur that are probably not accounted for. See lines 88 to 98 in the code below. Are those correct?
// does you code include the layerID for poles?
var LayerIdPoles = 0; // what is the layerID for poles?
var polesLayer =  FeatureSetByPortalItem(Portal('my portal'), 'xxxxxxxx', LayerIdPoles);
Console('polesLayer ' + polesLayer);

// does you code include the layerID for chambers?
var LayerIdChambers = 0; // what is the layerID for chambers?
var chambers  = FeatureSetByPortalItem(Portal('my portal'), 'xxxxxxxx', LayerIdChambers);
Console('chambers  ' + chambers);

// create buffer around feature of 0.1 meter
var buff = Buffer($feature, 0.1, 'meters');

// intersect buffer with poles
var poles = Intersects(buff, polesLayer);
var cnt = Count(poles);
Console('cnt ' + cnt);

// intersect buffer with chambers
var chambers = Intersects(buff, chambers);
var cntchambers = Count(chambers);
Console('cntchambers' + cntchambers);

var popupResult = '_'; // why is this an underscore?
// validate count before the loop
if (cnt>0) {
    for (var f in poles) {
        popupResult += f.barcode + " to pole ";
    }
    // only if you have poles, the following is relevant
    var strLenght = Count(popupResult) - 8;
    Console('strLenght ' + strLenght);
    var note = Left(popupResult, strLenght);
    Console('note ' + note)
    var popup = IIf(cnt==1, 
                    Replace(
                        "install subduct " + DomainName($feature, "sub_size") + " from pole " + note, 
                        'to pole',
                        ''), 
                    "install subduct " + DomainName($feature, "sub_size") + " from pole " + note);
    Console('popup '+popup);
} else {
    popupResult = "There is no Link required here";
    var popup = "";
}
Console('popupresult ' + popupResult);

var ChambResult = '_';
if (cntchambers>0) {
    for (var f in chambers) {
        ChambResult += f.label + " to chamber " ;
    }
    // only if you have chambers, the following is relevant
    var strLenght2 = Count(ChambResult) - 15;
    Console('strLenght2 ' + strLenght);
    var note2 = Left(ChambResult,strLenght2);
    Console('note2 '+note2);
    var popup2 = IIf(cntchambers==1, 
                    Replace(
                        "install subduct " + DomainName($feature,"sub_size")+ " from chamber " + note2,
                        'to chamber',
                        ''), 
                    "install subduct " + DomainName($feature,"sub_size") + " from chamber " + note2);
    Console('popup2 ' + popup2);
} else {
    ChambResult = "There is no Link required here";
    var popup2 = "";
}
Console('ChambResult ' + ChambResult);


var popup3 = "install subduct " + DomainName($feature,"sub_size") + 
             " from the chamber " + ChambResult + " to the pole " + popupResult;
Console('popup3 ' + popup3);

var popup3 = Replace(popup3,' to pole ', "");
Console('popup3 ' + popup3);
var popup3 = Replace(popup3,' to chamber ',"");
Console('popup3 '+popup3);

var finalresult = When(cnt <= 2 && cntchambers==0, popup,
                       cnt == 0 && cntchambers==2, popup2,
                       cnt == 1 && cntchambers==1, popup3,
                       'n/a');
Console('finalresult ' + finalresult);
return finalresult;

// 0 poles and 0 chambers -> 'n/a'
// 1 pole and 0 chambers -> popup
// 2 poles and 0 chambers -> popup
// 3 poles and 0 chambers -> 'n/a'
// 0 poles and 1 chambers -> 'n/a'
// 0 poles and 2 chambers -> popup2
// 0 poles and 3 chambers -> 'n/a'
// 1 pole and 1 chambers -> popup3
// 1 pole and 2 chambers -> 'n/a'
// 2 poles and 1 chambers -> 'n/a'
// 2 poles and 2 chambers -> 'n/a'

It is strange that if you only have a single feature, the expression works correctly but the field calculation fails. how are you filtering the featureset? Did you create a new source? It is possible that the pop-up validator is more forgiving than the one used in the field calculation...

View solution in original post

0 Kudos
14 Replies
XanderBakker
Esri Esteemed Contributor

Hi Paul Sweeney ,

This type of error is bound to happen if you don't include validations to see if you have a result to work with. It probably refers to the count executed on line 6 or 7. Make sure that you have access to the two layers you retrieve using FeatureSetByPortalItem on lines 1 and 2 and that you actually have valid poles and chambers.

In the pop-up it will execute only 1 example to show the result, but maybe somewhere in your data you may have an invalid feature that causes the error. 

PaulSweeney3
Occasional Contributor III

Hi Xander Bakker

Thanks for your comments , i have added console messages to the expression and everything returns the expected results including counts in line 6 and 7 . Is it the fact that if it returns a zero in some instances that is causing the error?  i have updated the expression to return a value if the count is zero.  I have also  filtered that table down to one feature and it is still not working on that feature even though the pop up returns the expected result. What would count as an invalid feature ?

Var polesLayer =  FeatureSetByPortalItem(Portal('my portal'), 'xxxxxxx')
console('polesLayer '+polesLayer )
var chambers  = FeatureSetByPortalItem(Portal('my portal'), 'xxxxxxx')
console('chamnbers  '+chambers )

Var buff = buffer($feature,.1,'meters')
console('buff '+buff)

Var poles = Intersects(buff,polesLayer)
console('poles  '+poles )

Var chambers  = Intersects(buff,chambers)
var cnt = count(poles)
console('cnt '+cnt)
var cntchambers = count(chambers)
console('cntchambers'+cntchambers)
var popupResult = '_'
for (var f in poles) {
    if (cnt>0) {
        popupResult += f.barcode + " to pole " ;
    } else {
            popupResult = "There is no Link required here";
        }
}
console('popupresult '+popupResult)
var ChambResult = '_'
for (var f in chambers) {
    if (cntchambers>0) {
        ChambResult += f.label + " to chamber " ;
    } else {
            ChambResult = "There is no Link required here";
        }
}
console('ChambResult '+ChambResult)
var strLenght = count(popupResult)-8
console('strLenght '+strLenght)
var note = left(popupResult,strLenght)
console('note '+note)
var strLenght2 = count(ChambResult)-15
console('strLenght2 '+strLenght)
var note2 = left(ChambResult,strLenght2)
console('note2 '+note2)
var popup = IIf(cnt==1, replace(("install subduct " + DomainName($feature,"sub_size")+ " from pole " +text(note)),'to pole',''), ("install subduct " + DomainName($feature,"sub_size")+ " from pole " +text(note)))
console('popup '+popup)
var popup2 = IIf(cntchambers==1, replace(("install subduct " + DomainName($feature,"sub_size")+ " from chamber " +text(note2)),'to chamber',''), ("install subduct " + DomainName($feature,"sub_size")+ " from chamber " +text(note2)))
console('popup2 '+popup2)
var popup3 = "install subduct " + DomainName($feature,"sub_size")+" from the chamber "+ChambResult+" to the pole "+popupResult
console('popup3 '+popup3)
var popup3 = replace(popup3,' to pole ',"")
console('popup3 '+popup3)
var popup3 = replace(popup3,' to chamber ',"")
console('popup3 '+popup3)
var finalresult = when(cnt <= 2 && cntchambers==0,popup,cnt == 0 && cntchambers==2, popup2,cnt == 1 && cntchambers==1, popup3,'n/a')
console('finalresult '+finalresult)
return finalresult‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Paul Sweeney ,

Sorry for the late reply but I was attending the IMGIS conference and yesterday was a national holiday.

I just changed your code slightly, but it is still not very convincing. A couple of comments:

  • In your code do you have the layerID when you use FeatureSetByPortalItem for poles and chambers?
  • The validation of the count is before you do the loop for both poles and chambers
  • I am not convinced with the final When, since there are a number of cases that in theory can occur that are probably not accounted for. See lines 88 to 98 in the code below. Are those correct?
// does you code include the layerID for poles?
var LayerIdPoles = 0; // what is the layerID for poles?
var polesLayer =  FeatureSetByPortalItem(Portal('my portal'), 'xxxxxxxx', LayerIdPoles);
Console('polesLayer ' + polesLayer);

// does you code include the layerID for chambers?
var LayerIdChambers = 0; // what is the layerID for chambers?
var chambers  = FeatureSetByPortalItem(Portal('my portal'), 'xxxxxxxx', LayerIdChambers);
Console('chambers  ' + chambers);

// create buffer around feature of 0.1 meter
var buff = Buffer($feature, 0.1, 'meters');

// intersect buffer with poles
var poles = Intersects(buff, polesLayer);
var cnt = Count(poles);
Console('cnt ' + cnt);

// intersect buffer with chambers
var chambers = Intersects(buff, chambers);
var cntchambers = Count(chambers);
Console('cntchambers' + cntchambers);

var popupResult = '_'; // why is this an underscore?
// validate count before the loop
if (cnt>0) {
    for (var f in poles) {
        popupResult += f.barcode + " to pole ";
    }
    // only if you have poles, the following is relevant
    var strLenght = Count(popupResult) - 8;
    Console('strLenght ' + strLenght);
    var note = Left(popupResult, strLenght);
    Console('note ' + note)
    var popup = IIf(cnt==1, 
                    Replace(
                        "install subduct " + DomainName($feature, "sub_size") + " from pole " + note, 
                        'to pole',
                        ''), 
                    "install subduct " + DomainName($feature, "sub_size") + " from pole " + note);
    Console('popup '+popup);
} else {
    popupResult = "There is no Link required here";
    var popup = "";
}
Console('popupresult ' + popupResult);

var ChambResult = '_';
if (cntchambers>0) {
    for (var f in chambers) {
        ChambResult += f.label + " to chamber " ;
    }
    // only if you have chambers, the following is relevant
    var strLenght2 = Count(ChambResult) - 15;
    Console('strLenght2 ' + strLenght);
    var note2 = Left(ChambResult,strLenght2);
    Console('note2 '+note2);
    var popup2 = IIf(cntchambers==1, 
                    Replace(
                        "install subduct " + DomainName($feature,"sub_size")+ " from chamber " + note2,
                        'to chamber',
                        ''), 
                    "install subduct " + DomainName($feature,"sub_size") + " from chamber " + note2);
    Console('popup2 ' + popup2);
} else {
    ChambResult = "There is no Link required here";
    var popup2 = "";
}
Console('ChambResult ' + ChambResult);


var popup3 = "install subduct " + DomainName($feature,"sub_size") + 
             " from the chamber " + ChambResult + " to the pole " + popupResult;
Console('popup3 ' + popup3);

var popup3 = Replace(popup3,' to pole ', "");
Console('popup3 ' + popup3);
var popup3 = Replace(popup3,' to chamber ',"");
Console('popup3 '+popup3);

var finalresult = When(cnt <= 2 && cntchambers==0, popup,
                       cnt == 0 && cntchambers==2, popup2,
                       cnt == 1 && cntchambers==1, popup3,
                       'n/a');
Console('finalresult ' + finalresult);
return finalresult;

// 0 poles and 0 chambers -> 'n/a'
// 1 pole and 0 chambers -> popup
// 2 poles and 0 chambers -> popup
// 3 poles and 0 chambers -> 'n/a'
// 0 poles and 1 chambers -> 'n/a'
// 0 poles and 2 chambers -> popup2
// 0 poles and 3 chambers -> 'n/a'
// 1 pole and 1 chambers -> popup3
// 1 pole and 2 chambers -> 'n/a'
// 2 poles and 1 chambers -> 'n/a'
// 2 poles and 2 chambers -> 'n/a'

It is strange that if you only have a single feature, the expression works correctly but the field calculation fails. how are you filtering the featureset? Did you create a new source? It is possible that the pop-up validator is more forgiving than the one used in the field calculation...

0 Kudos
PaulSweeney3
Occasional Contributor III

Hi @XanderBakker 

My turn to apologise for the late response , i have tried you expression and i am still getting the same error unfortunately . Just in response to some of your comments  

The when statement , in theory there should only be 2 features intersecting the line at either end and it should be a pole or a chamber so 5 potential scenarios. Would this account for the error ? where it is  only 1 pole or chamber  i don't need a value i thought this would return just 'n/a'

  • 1 pole 1Pole (cnt <= 2 && cntchambers==0)
  • 1 Pole 'n/a'
  • 1 Pole 1 chamber (cnt == 1 && cntchambers==1)
  • 1 chamber 1chamber (cnt == 0 && cntchambers==2)
  • 1 chamber 'n/a'

 

// does you code include the layerID for poles?

Yes my code contains the layer ID for the poles and chambers 

why is this an underscore?

No major reason  had it as a blank but then i thought perhaps the blank value was creating the issue so i just tried it with an underscore 

 

I filtered it down to one objectid i knew worked with the pop up?

A new source? do you mean re publish the feature layers ? 

 

Thanks for your help on this 

Regards 

 

 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @PaulSweeney3 ,

I guess the easiest way to clear up some doubts would be to have access to the data and track down the error. Is this a possibility? I possible create a group and share the web map and layers to that group and invite me to that group using my AGOL user "xbakker.spx".

0 Kudos
PaulSweeney3
Occasional Contributor III

@XanderBakker 

 

@XanderBakker  Again apologies for the delayed response i have set up a group and added you with the layers and a map with the expression included. Please feel free to investigate if you have an opportunity , thanks for your help on this . 

XanderBakker
Esri Esteemed Contributor

Hi @PaulSweeney3 ,

Thanks for sharing the data. I noticed that you didn't include the web map so I don't have access to any other Arcade expression configured in the pop-up. I used the code I posted earlier and just changed to way to access the poles and chambers and this is returned in the pop-up:

XanderBakker_0-1606397956463.png

Can you indicate what should be changed?

 

0 Kudos
PaulSweeney3
Occasional Contributor III

Hi @XanderBakker  yes this is expected it works for me in the pop up as well however when i try to calculate a field with  the same expression  it doesn't work. i shared the test map in there now but i understand you do not have the ability to calculate fields via arcade 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @PaulSweeney3 ,

 

Let me validate if I can perform the field calculation in ArcGIS Online. Can you confirm the output field name that should be filled with the result? If it does not work on ArcGIS Online, I can give it a shot and try to connect with ArcGIS Pro and perform the field calculation in Pro. Not sure if you have tried this already?

0 Kudos