I’ve run into an exception trying to create a FeatureLayer with the results of a spatial query. The exception stack trace looks like this:
TypeError: Circular reference in value argument not supported
at b.toJson
at _initLayer
at Anonymous function
at _initFeatureLayer
at constructor
at h.around.advice
at h
at Anonymous function
at _createResultsFeatureLayer (my function)
I’ve found several posts that describe circular reference issues with serializing graphics to JSON. Unfortunately none of them seem to fit my predicament. When I look at the FeatureLayer object, all of the graphics contain a reference for the _graphicsLayer, which includes the same collection of graphics with each graphic containing a reference to the _graphicsLayer, etc. Seems infinitely recursive/circular. "The wheels on the bus go ‘round and ‘round…" I am not sure if this is the issue or not – it could be completely unrelated. In any case, I’m not sure how to break the cycle or resolve the exception. Hopefully someone out there might have an answer, or at least some suggestions for things to try.
The attached zip contains a custom widget “CircularReference” that can be used to reproduce the problem.
To include the widget:
"widgetPool": {
"widgets": [
{
"uri": "widgets/CircularReference/Widget",
"version": "1.3",
"id": "widgets/CircularReference/Widget_35",
"name": "CircularReference",
"label": "Circular Reference",
"index": 2
},
"searchLayers": [
{
"mapServiceName": "USA",
"name": "Cities",
"url": "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0"
},
{
"mapServiceName": "USA",
"name": "Highways",
"url": "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/1"
},
{
"mapServiceName": "USA",
"name": "States",
"url": "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/2"
},
{
"mapServiceName": "USA",
"name": "Counties",
"url": "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/3"
}
],
After getting everything configured, the workflow to generate the exception is as follows:
Any feedback, thoughts, or suggestions would be greatly appreciated.
Thank you in advance!
Gary
Solved! Go to Solution.
The solution to my issue was to create copies of the features found by the query, and then use the set of copied features when creating the feature layer. One of Kelly Hutchins' posts put me on the right track (thank you Kelly!).
The following bold lines in the following code snippet show the changes I made to the _createResultsFeatureLayer function in the code attached to the original post.
_createResultsFeatureLayer: function (selectedResultsLayerItem) {
//--- Kelly Hutchins forum post with example creating feature layer with layer definition:
//--- https://community.esri.com/thread/99683
// AVOID CIRCULAR REFERENCE EXCEPTION IN FeatureLayer CONSTRUCTOR BY MAKING
// COPIES OF THE RESULTS, SPECIFICALLY COPIES OF THE RESULT GRAPHIC OBJECTS.
// BASED ON ANOTHER POST FROM KELLY WITH WORKAROUND FOR A CIRCULAR
// REFERENCE ISSUE WHEN PRINTING:
// https://community.esri.com/message/70485#70485
// THIS APPROACH WORKS. THANK YOU KELLY!
// use toJson() to create an independent COPY of each result feature/graphic.
// ONLY replacing the result's set of features, leaving all other properties of the
// original results object intact.
// MAKING *COPIES* OF THE ORIGINAL FEATURES IS THE KEY
// TO AVOIDING THE CIRCULAR REFERENCE EXCEPTION.
var copyFeatures = dojo.map(selectedResultsLayerItem.results.features, function (feature) {
return new esri.Graphic(feature.toJson());
});
selectedResultsLayerItem.results.features = copyFeatures;
var featureCollection = {
layerDefinition: selectedResultsLayerItem.layerDefinition,
featureSet: selectedResultsLayerItem.results
};
var featureLayer = new FeatureLayer(featureCollection,
{
mode: FeatureLayer.MODE_ONDEMAND,
id: featureLayerId
});
The solution to my issue was to create copies of the features found by the query, and then use the set of copied features when creating the feature layer. One of Kelly Hutchins' posts put me on the right track (thank you Kelly!).
The following bold lines in the following code snippet show the changes I made to the _createResultsFeatureLayer function in the code attached to the original post.
_createResultsFeatureLayer: function (selectedResultsLayerItem) {
//--- Kelly Hutchins forum post with example creating feature layer with layer definition:
//--- https://community.esri.com/thread/99683
// AVOID CIRCULAR REFERENCE EXCEPTION IN FeatureLayer CONSTRUCTOR BY MAKING
// COPIES OF THE RESULTS, SPECIFICALLY COPIES OF THE RESULT GRAPHIC OBJECTS.
// BASED ON ANOTHER POST FROM KELLY WITH WORKAROUND FOR A CIRCULAR
// REFERENCE ISSUE WHEN PRINTING:
// https://community.esri.com/message/70485#70485
// THIS APPROACH WORKS. THANK YOU KELLY!
// use toJson() to create an independent COPY of each result feature/graphic.
// ONLY replacing the result's set of features, leaving all other properties of the
// original results object intact.
// MAKING *COPIES* OF THE ORIGINAL FEATURES IS THE KEY
// TO AVOIDING THE CIRCULAR REFERENCE EXCEPTION.
var copyFeatures = dojo.map(selectedResultsLayerItem.results.features, function (feature) {
return new esri.Graphic(feature.toJson());
});
selectedResultsLayerItem.results.features = copyFeatures;
var featureCollection = {
layerDefinition: selectedResultsLayerItem.layerDefinition,
featureSet: selectedResultsLayerItem.results
};
var featureLayer = new FeatureLayer(featureCollection,
{
mode: FeatureLayer.MODE_ONDEMAND,
id: featureLayerId
});