Attributes not included in HitTestResult

1046
2
Jump to solution
08-22-2019 12:34 PM
ShaneBuscher1
New Contributor III

I'm performing a MapView.hitTest() against a feature layer in a webmap and running across inconsistencies with attributes being returned. I get a reference to the feature layer and set the outFields I want returned. However, sometimes the graphic that is 'hit' only returns the ObjectId. Other times it returns all attributes specified in outFields. 

view.on("click", function (event) {

  view.hitTest(event).then(function (response) {

    if (response.results.length) {

      var graphic = response.results.filter(function (result) {

        return result.graphic.layer === myLayer;

      })[0].graphic;
         

      // graphic.attributes only contains objectid
      // OR
      // graphic.attributes contains objectid, att1, att2, att3, etc...

     }

  });

});

What is the expected behavior? For instance, should you assume that you may only get back an ObjectId and not additional attributes set through FeatureLayer.outFields? Does hitTest ignore outFields? 

1 Solution

Accepted Solutions
ShaneBuscher1
New Contributor III

After some deeper digging the answer lies in the Loadable pattern. Below is the solution where each feature layer in the web map has its outFields property set to 'all' with the splat. The 'load' function initializes the feature layers the way I need BEFORE adding to the mapView. The issue I had earlier dealt with a lazy-loaded component that was setting the outFields AFTER the webMap and mapView instances had loaded and rendered. 

let mapView = new MapView({ 
  container: 'mapEl',
});

let webMap = new WebMap(props);

webMap.load()
 .then(() => {
   webMap.layers
     .filter(layer => { return layer.type === 'feature' })
     .map(layer => {
       let featLayer = <esri.FeatureLayer>layer; 
       featLayer.outFields = ['*'];
       return featLayer; 
     });
 });
 
mapView.map = webMap;

View solution in original post

2 Replies
ShaneBuscher1
New Contributor III

After some deeper digging the answer lies in the Loadable pattern. Below is the solution where each feature layer in the web map has its outFields property set to 'all' with the splat. The 'load' function initializes the feature layers the way I need BEFORE adding to the mapView. The issue I had earlier dealt with a lazy-loaded component that was setting the outFields AFTER the webMap and mapView instances had loaded and rendered. 

let mapView = new MapView({ 
  container: 'mapEl',
});

let webMap = new WebMap(props);

webMap.load()
 .then(() => {
   webMap.layers
     .filter(layer => { return layer.type === 'feature' })
     .map(layer => {
       let featLayer = <esri.FeatureLayer>layer; 
       featLayer.outFields = ['*'];
       return featLayer; 
     });
 });
 
mapView.map = webMap;
rogenjh
New Contributor III

8)  saved my butt late on a Friday!

0 Kudos