QueryTask.execute in a Dojo class

2530
2
03-27-2014 06:37 AM
ChristianDebono
New Contributor II
I have written a class with a method which returns the coordinates of a point.

define(["dojo/_base/declare", "esri/tasks/QueryTask", "esri/tasks/query", "dojo/dom"],

    function (declare, QueryTask, Query, dom) {
        return declare(null, {

            meterId: null,
            featureUrl: null,
            queryString: null,

            constructor: function (options) {
                this.featureUrl = options.featureUrl;
                this.meterId = options.meterId;
                this.queryString = options.queryString;
            },

            doFind: function () {
                var meterCoor = "";
                var query = new Query();
                var queryTask = new QueryTask(this.featureUrl);
                query.where = this.queryString + " = '" + this.meterId + "'";
                query.returnGeometry = true;
                query.outFields = ["*"];

                queryTask.execute(query, function (results) {
                    for (var x = 0; x < results.features.length; x++) {
                        meterCoor = results.features.geometry.x + "," + results.features.geometry.y;
                    }
                });
                
                return meterCoor;
            }
        });
    });


My problem is that "meterCoor" is being populated, but when I'm returning it the variable is left empty. I can't figure out what is wrong with my code. I'll appreciate any help given to me.
0 Kudos
2 Replies
BenFousek
Occasional Contributor III
I'm not sure why you are iterating the results when I assume you are returning a single feature. Second queryTask.execute is a deferred. Your return meterCoor; is being returned before the task is executed.

Try:
queryTask.execute(query, function (results) {
    return results.features[0].geometry.x + "," + results.features[0].geometry.y;
}, function (e) {
    console.log(e);
    return e;
});


You can also create your own deferred and handle results or error outside the class:
doFind: function () {
    var deferred = new Deferred();
    var query = new Query();
    var queryTask = new QueryTask(this.featureUrl);
    query.where = this.queryString + " = '" + this.meterId + "'";
    query.returnGeometry = true;
    query.outFields = ["*"];
    queryTask.execute(query, function (results) {
        deferred.resolve(results);
    }, function (e) {
        console.log(e);
        deferred.reject('something has gone wrong');
    });
    return deferred;
}
0 Kudos
BenFousek
Occasional Contributor III
It looks like your class is a utility-like class. Maybe you have more methods and such you didn't share, but you can forgo declare and just return an object. Then you don't need to create the class to use it, instead just use its methods directly:
define([
    "esri/tasks/QueryTask",
    "esri/tasks/query"
], function (
    QueryTask,
    Query
) {
    return {
        doFind: function (featureUrl, meterId, queryString) {
            //do stuff
        },
        doSomethingElse: function (a, b) {
            //do stuff
        }
    };
});


require(['myClasses/utility'], function (utility) {
    utility.doFind(a, b, c);
});
0 Kudos