3

Using the ArcGIS JavaScript API 4.14 to query a feature service on a server that is federated to my instance of Portal/ArcGIS Enterprise. No matter what I do the queryTask.execute() is returning an empty array.

I'm using IWA to authenticate to my Portal and OAuth2 to generate a token, which queryTask() is appending to the request which looks like this: <URL>/query/f=json&returnGeometry=false&spatialRel=esriSpatialRelIntersects&where=1%3D1&token=[tokengibberish] (that's an example where I was desperate and using a 1=1 in the where clause, just trying to get it to return SOMETHING).

My script uses hitTest() to pull an OBJECTID from a feature on the map and stores that OBJECTID to a variable:

view.on("click", function (event) {
    view.hitTest(event).then(function (response) {
        if (response.results.length) {
            let graphic = response.results.filter(function (result) {
                return result.graphic.layer === station;
                    })[0].graphic;

            // do something with the result graphic
            console.log(graphic.attributes.OBJECTID);

            document.getElementById("displaytext").innerHTML = graphic.attributes.OBJECTID;

            feature_objectid = graphic.attributes.OBJECTID;
        }
    });
});

Then I have a function built to listen for a button click event that executes the queryTask:

view.when(function() {
    document.getElementById("querybutton").addEventListener("click", 
        function(event) {

            var queryTask = new QueryTask({
                url: station.url  // Station is the FeatureLayer that is displayed on the map and the OBJECTID is pulled from using hitTest()
            });

            var query = new Query();
            query.where = "OBJECTID=" + feature_objectid;  //feature_objectid comes from the result of the hitTest()

            console.log("Query: " + query.where);

            queryTask.execute(query).then(function(results){
                //Down here I'm just trying to understand the output        
                console.log('Query',query);
                console.log(results);
                console.log(results.features);
            });
        }); // Close event listener
 }); // Close button view.when

No matter what I put into query.where the results are always {"layers":[]}

The URL that is sent to the server looks like this: https://<serviceURL>/FeatureServer/query?f=json&returnGeometry=false&spatialRel=esriSpatialRelIntersects&where=OBJECTID%3D64201&token=[tokengibberish]

I can successfully run a query from the /FeatureServer/query/ page by writing in the Layer Definition manually. I have even had success using PostMan to generate a successful query by pointing at the REST endpoint and manually copying in the token and setting the layer definition - so I know things are working I just can't seem to get my code in order.

7
  • What does the url look like that it's sending to the server? – Kirk Kuykendall Jan 21 '20 at 22:47
  • See my edit - I added a copy of the URL that is being passed to the server. – Kevin Jan 21 '20 at 23:09
  • Verify the query request sent to ArcGIS Server by making 'f=html' instead of 'f=json' and then open the updated URL in a new tab. – Mayur Shinde Jan 22 '20 at 9:43
  • 1
    I might be wrong, but I think queryTask works on a layer of a map, it should be FeatureServer/query/X where X is the layer number. – cabesuon Jan 22 '20 at 10:59
  • @cabesuon, in that scenario it has to be somewhat like FeatureServer/X/query where X is the Feature layer number. – Mayur Shinde Jan 22 '20 at 13:17
1

The URL that is sent to the server which is: https://serviceURL/FeatureServer/query?f=json&returnGeometry=false&spatialRel=esriSpatialRelIntersects&where=OBJECTID%3D64201&token=[tokengibberish]

This URL must have the feature layer ID after the 'FeatureServer'. Your application is sending this request to server as the url parameter in QueryTask constructor might not have the feature layer ID.

var queryTask = new QueryTask({
                url: station.url  // Station is the FeatureLayer that is displayed on the map and the OBJECTID is pulled from using hitTest()
            });

This url parameter requires an url like : "https://serviceURL/FeatureServer/0"

You can refer this sample, in which on line 55 the url is declared and used on line number 139.

2
  • I'm really glad to have this working. I'm kicking myself that it was something so simple! Much appreciate the help! – Kevin Jan 22 '20 at 17:10
  • @Kevin , pleasure helping you out. – Mayur Shinde Jan 23 '20 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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