2

I am new to JavaScript and to JavaScript APIs as well,

I am trying to get all the attributes of a feature that is clicked on the webmap, and I don't want to use the default pop up so I disabled it, and wrote the function below. I get the geometry, but I want the actual feature.

How can I do this?

Here is the current code I have which always returns 0 features when I do the query based on the geometry or mappoint.

 view.on("click", async function(event){
        view.hitTest(event).then(
            async function(response){
            for(let j=0;j<=response.results.length-1;j++){
                var graphic = response.results[j].graphic;
                
                let featureLayer=graphic.layer;
                let lyrType  =featureLayer.operationalLayerType;
                if(lyrType!='ArcGISFeatureLayer')
                    continue;
                var query = featureLayer.createQuery();
                query.geometry = graphic.geometry; // I also tried event.mapPoint
                query.outFields= ["*"];
                query.spatialRelationship = "intersects";
                let rr= await featureLayer.queryFeatures(query);
                let numberOfSelectedFeatures=rr.features.length;  // this is 0, unless i remove the query then it gets every feature in the layer          
                }
         
        });
    });

1 Answer 1

1

I don't understand why you use functions async way, but there is an example in the documentation.

HitTest sample


view.on("pointer-down", eventHandler);
function eventHandler(event) {
  view.hitTest(event).then(function (response) {
    if (response.results.length) {
      const graphic = response.results[0].graphic;
      const attributes = graphic.attributes;
      console.log(attributes);
    }
  });
}

If FeatureLayer' features have attributes and you want to get all attributes, you should define outFields property as ["*"].

const layer = new FeatureLayer({
  // URL to the service
  url: "url,
  outFields: ["*"]
});

OR

featureLayer.outFields = ["*"];

3
  • Thanks for your input, however as you can see from my code, I already do this, but I want to access the feature not the graphic, the attributes that are retrieved from graphic are not all the attributes of the feature. So I want to access the Feature, not the Graphic so I can load it, and get all it's attributes.
    – ZZZ
    Commented Jan 23, 2022 at 9:21
  • Can you try defining outFields on layer? If you want to get all attributes you should define outfields like; featureLayer.outFields = ["*"] developers.arcgis.com/javascript/latest/api-reference/… . If this works, I will edit my answer. It's work on my sample. @ZZZ
    – berkayoruc
    Commented Jan 23, 2022 at 16:34
  • The outfields are set to be * in the Webmap, they all appear when using the regular default esri popup. I don't know why the response graphic (Selected Feature on the map) only gives 2 attributes, ObjectId and Shape. I decided to query the feature using the object id and it worked, but I think there is something weird going on because in my mind I shouldn't have to do all of this to get the feature
    – ZZZ
    Commented Jan 24, 2022 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.