Attributes not included in HitTestResult

2192
4
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

4 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

😎  saved my butt late on a Friday!

TimDietz
New Contributor III

Thank you.  This worked for me.  And, I will add that it was some change that occurred from version 4.13 to 4.14.

LorenzoMara
New Contributor

Thanks, it really worked for me. Only ObjectID was shown to me

0 Kudos