I'm getting a ReferenceError "results is not defined" on the queryTask.execute:
//first, check to see if a workplan boundary exists
if (this.workplanId) {
var queryUrl = "https://somewebsite/someagssite/rest/services/folder/servicename/FeatureServer/0"
var queryTask = new QueryTask(queryUrl);
var query = new Query();
query.returnGeometry = true;
query.where = "workplanId ='" + this.workplanId + "'";
debugger;
queryTask.execute(query).then(function (results) {
this.workplanCheckValue = results.features.length;
});
}
console.log("workplanCheck value is: ", this.workplanCheckValue)
I need to set this.workplanCheckValue to the number of features returned from the queryTask.
Any assistance appreciated!
Solved! Go to Solution.
James,
I tend to do my queries like this:
////first, check to see if a workplan boundary exists
if (this.workplanId) {
var queryUrl = "https://somesite/rest/services/myservice/FeatureServer/0";
var queryTask = new QueryTask(queryUrl);
var query = new Query();
query.returnGeometry = true;
query.outFields = ['*']
query.where = "workplanId ='" + this.workplanId + "'";
debugger;
queryTask.execute(query, lang.hitch(this, function (result) {
debugger;
this.workplanCheckValue = result.features.length;
console.log("workplanCheck value is: ", this.workplanCheckValue)
}));
}
console.log("workplanCheck value is: ", this.workplanCheckValue)
James,
Looks like a scope issue again. When you are inside a function that does not use lang.hitch or a closure then "this" has a different scope then outside the function.
I'm struggling on this whole concept for some reason. It's just not registering how to apply and where -- my failed attempt:
queryTask.execute(query).then(lang.hitch(function (result) {
this.workplanCheckValue = result.features.length;
}));
James,
OK maybe I can help explain. Anytime you have some code that has a function that returns something that means it returns a different scope and thus has to be hitched to the current scope.
queryTask.execute(query).then(lang.hitch(this, function (result) {
this.workplanCheckValue = result.features.length;
}));
Your where missing the "this" that you were wanting to hitch to.
Now it leads me to a "TypeError: g.join is not a function"? Something new. <-- this appears to be because my query.outFields not correctly set as an array.
However, I'm still getting the same error on my original code:
////first, check to see if a workplan boundary exists
if (this.workplanId) {
var queryUrl = "https://somesite/rest/services/myservice/FeatureServer/0";
var queryTask = new QueryTask(queryUrl);
var query = new Query();
query.returnGeometry = true;
query.outFields = ['*']
query.where = "workplanId ='" + this.workplanId + "'";
debugger;
queryTask.execute(query).then(lang.hitch(this, function (result) {
debugger;
this.workplanCheckValue = result.features.length;
console.log("workplanCheck value is: ", this.workplanCheckValue)
}));
}
console.log("workplanCheck value is: ", this.workplanCheckValue)
Thanks again for your explanation!
Is that error coming from the function above?...
Sorry -- I was too slow. I updated my last reply. That other error was due to
query.outFields = ['*']
This is what I see on result when in debug:
James,
I tend to do my queries like this:
////first, check to see if a workplan boundary exists
if (this.workplanId) {
var queryUrl = "https://somesite/rest/services/myservice/FeatureServer/0";
var queryTask = new QueryTask(queryUrl);
var query = new Query();
query.returnGeometry = true;
query.outFields = ['*']
query.where = "workplanId ='" + this.workplanId + "'";
debugger;
queryTask.execute(query, lang.hitch(this, function (result) {
debugger;
this.workplanCheckValue = result.features.length;
console.log("workplanCheck value is: ", this.workplanCheckValue)
}));
}
console.log("workplanCheck value is: ", this.workplanCheckValue)
Oh actually, that does work. My fault (again).