Select to view content in your preferred language

Experience Builder Arcade Code Error "Cannot access value using a key of this type"

301
3
3 weeks ago
TimWill
New Contributor

Here is my code to sum the length field times the width field to get the total area for line features.

var fs = $dataSources["dataSource_1-19d7400ea8f-layer-20"];
var total = 0;
for (var f in fs) {
  var len_in_inches = f.Shape.STLength() * 12;
  var width_in_inches = Number(f.Width_Inches, 0);
  total += width_in_inches * len_in_inches;
}
return total;
 
I get the "Cannot access value using a key of this type." error, but can't figure out how to fix.
0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor

The first line of your script doesn't return a FeatureSet, but rather a dictionary. You have to use the layer property to get the features from $dataSource.

var fs = $dataSources["dataSource_1-19d7400ea8f-layer-20"].layer;
var total = 0;
for (var f in fs) {
  var len_in_inches = f.Shape.STLength() * 12;
  var width_in_inches = Number(f.Width_Inches, 0);
  total += width_in_inches * len_in_inches;
}
return total;

 

0 Kudos
TimWill
New Contributor

Thanks for the response. I am getting a new error with the f.Shape callout.

"Key not found - Shape."

Not sure why, but I did notice your code only has the STLength() in red and not the entire name.

My field is the esri feature class field Shape.STLength() field.

Thanks in advance for any additional assistance.

 

0 Kudos
KenBuja
MVP Esteemed Contributor

I use the JavaScript language selection in the code sample window for Arcade code, which highlights code in a different way than the language that you selected.

When working with a normal FeatureSet, you should be able to return the length of a feature using "f.Shape_Leng". If that doesn't work with your feature, I would suggest using Console function to examine the schema and its available fields.

console(Schema(fs).fields)

 

0 Kudos