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.
1 Answer 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.
-
I'm really glad to have this working. I'm kicking myself that it was something so simple! Much appreciate the help!Kevin– Kevin2020年01月22日 17:10:46 +00:00Commented Jan 22, 2020 at 17:10
-
@Kevin , pleasure helping you out.Mayur Shinde– Mayur Shinde2020年01月23日 06:29:02 +00:00Commented Jan 23, 2020 at 6:29
FeatureServer/query/X
whereX
is the layer number.