queryTask.execute Fails

983
12
Jump to solution
04-27-2018 09:06 AM
JamesCrandall
MVP Frequent Contributor

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!

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

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)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
12 Replies
RobertScheitlin__GISP
MVP Emeritus

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.

JamesCrandall
MVP Frequent Contributor

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;
                }));
               
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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.

JamesCrandall
MVP Frequent Contributor

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!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Is that error coming from the function above?...

0 Kudos
JamesCrandall
MVP Frequent Contributor

Sorry -- I was too slow.  I updated my last reply.  That other error was due to

query.outFields = ['*']

0 Kudos
JamesCrandall
MVP Frequent Contributor

This is what I see on result when in debug:

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
JamesCrandall
MVP Frequent Contributor

Oh actually, that does work.  My fault (again).

0 Kudos