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.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Jan 21, 2020 at 21:43
7
  • What does the url look like that it's sending to the server? Commented Jan 21, 2020 at 22:47
  • See my edit - I added a copy of the URL that is being passed to the server. Commented Jan 21, 2020 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. Commented Jan 22, 2020 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. Commented Jan 22, 2020 at 10:59
  • @cabesuon, in that scenario it has to be somewhat like FeatureServer/X/query where X is the Feature layer number. Commented Jan 22, 2020 at 13:17

1 Answer 1

2

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.

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.