Select to view content in your preferred language

Switch statement JS

7874
32
Jump to solution
05-27-2014 10:34 AM
AlexGole1
Emerging Contributor
Hi all,
I am trying to create a GIS data portal that allows users to select one out three diffent methods for data extraction.
1) extract by graphic extent
2) extract by extent of a county
3) extract by unit (calfire)

Here is my problem. I am using a switch statement that allow the users to select either one of these methods independently as shown on this thread.

However, when I trigger the either one of the cases within my function by pushing on a download button. It seems like the GPtool cant access any input data for some reasons.

errors I get for each of the functions described above :
TypeError: Unable to get property 'geometry' of undefined or null reference  TypeError: Unable to get property 'style' of undefined or null reference  TypeError: Unable to get property 'style' of undefined or null reference


My code:
var extractMethod = "extractByPoly";    var extractMethod = "extractByCounty";    var extractMethod = "extractByUnit";        function myFunction() {     //var extractMethod = "extractByPoly";     //var extractMethod = "extractByCounty";     //var extractMethod = "extractByUnit";      switch (extractMethod) {         default:              //function extractData() {             //get clip layers             var clipLayers = [];             if (registry.byId("layer1").get("checked")) {                 clipLayers.push("SRA");             }             if (registry.byId("layer2").get("checked")) {                 clipLayers.push("Burn");             }             if (clipLayers.length === 0 || map.graphics.graphics.length === 0) {                 alert("Select layers to extract and draw an area of interest.");                 return;             }              var featureSet = new FeatureSet();             var features = [];             features.push(map.graphics.graphics[0]);             featureSet.features = features;              var params3 = {                 "Layers_to_Clip": clipLayers,                 "Area_of_Interest": featureSet,                 "Feature_Format": registry.byId("formatBox").get("value")             };              domStyle.set(loading2, "display", "inline-block");             gp.submitJob(params3, completeCallback, statusCallback2, function (error) {                 alert(error);                 domStyle.set(loading2, "display", "none");             });             //}             break;          case "extractByCounty":             //function executeQueryTask(county) {             var clipLayers = [];             if (registry.byId("layer1").get("checked")) {                 clipLayers.push("SRA");             }             if (registry.byId("layer2").get("checked")) {                 clipLayers.push("Burn");             }             if (clipLayers.length === 0) {                 alert("No layers to clip, please select a layer you would like to clip...");                 return;             }              var county = document.getElementById("sel_county").value             var unit = document.getElementById("sel_unit").value             var queryTask = new QueryTask("http://webgisdevint1/arcgis/rest/services/Alex_Try/Counties/MapServer/0");                                       var query = new Query();             query.returnGeometry = true;             query.outFields = ["NAME_PCASE"];             query.where = "NAME_PCASE = '" + county + "'";             query.outSpatialReference = map.spatialReference;             queryTask.execute(query, function (featureSet) {                 var AOI = featureSet.features[0].geometry;                 var graphic = new Graphic(AOI);                 var features = [];                 features.push(graphic);                 var fSet = new FeatureSet();                 fSet.features = features;                   var params = {                     "Layers_to_Clip": clipLayers,                     "Area_of_Interest": fSet,                     "Feature_Format": registry.byId("formatBox").get("value")                 };                    domStyle.set(loading, "display", "inline-block");                 gp.submitJob(params, completeCallback, statusCallback, function (error) {                     alert(error);                     domStyle.set(loading, "display", "none");                 });             });             //}             break;           case "extractByUnit":             //function executeQueryTask1(unit) {             var clipLayers = [];             if (registry.byId("layer1").get("checked")) {                 clipLayers.push("SRA");             }             if (registry.byId("layer2").get("checked")) {                 clipLayers.push("Burn");             }             if (clipLayers.length === 0) {                 alert("No layers to clip, please select a layer you would like to clip...");                 return;             }              var unit = document.getElementById("sel_unit").value             var queryTask = new QueryTask("http://webgisdevint1/arcgis/rest/services/Alex_Try/Layers/MapServer/1");                          var query = new Query();             query.returnGeometry = true;             query.outFields = ["UNITCODE"];             query.where = "UNITCODE = '" + unit + "'";             query.outSpatialReference = map.spatialReference;             queryTask.execute(query, function (featureSet) {                 var AOI = featureSet.features[0].geometry;                 var graphic = new Graphic(AOI);                 var features = [];                 features.push(graphic);                 var fSet = new FeatureSet();                 fSet.features = features;                   var params2 = {                     "Layers_to_Clip": clipLayers,                     "Area_of_Interest": fSet,                     "Feature_Format": registry.byId("formatBox").get("value")                 };                    domStyle.set(loading1, "display", "inline-block");                 gp.submitJob(params2, completeCallback, statusCallback1, function (error) {                     alert(error);                     domStyle.set(loading1, "display", "none");                 });             });             //}     } }


Any idea why I get these errors?
0 Kudos
32 Replies
SteveCole
Honored Contributor
Since your app is an internal application, it's really hard for us on the outside to troubleshoot what you're seeing. Your description makes me think there's a "cart in front of the horse" situation whereby part of your code is trying to use an object before it's returned from a query. In other words, "e" might be null because the code is accessing it before it gets populated. I've been tripped up by this type of situation before and it's frustrating to remedy.

I tried pulling out your dependencies on the code and from what limited things I can do, I'm not experiencing an error like you posted. Line 205 also doesn't seem to make sense in the code I have in front of me. Where is that? Is that in the addGraphic function we just added a post or two ago?
0 Kudos
AlexGole1
Emerging Contributor
Since your app is an internal application, it's really hard for us on the outside to troubleshoot what you're seeing. Your description makes me think there's a "cart in front of the horse" situation whereby part of your code is trying to use an object before it's returned from a query. In other words, "e" might be null because the code is accessing it before it gets populated. I've been tripped up by this type of situation before and it's frustrating to remedy.

I tried pulling out your dependencies on the code and from what limited things I can do, I'm not experiencing an error like you posted. Line 205 also doesn't seem to make sense in the code I have in front of me. Where is that? Is that in the addGraphic function we just added a post or two ago?


You are right since it's not a public app it must be had to see what is going on. I attached my app with only the latest code and what I think is necessary for it to make it run. Some images might be missing. The only thing you wont have access to are the GP tool but you can use the sample GP tool here from this code, it does the same thing as mine:

https://developers.arcgis.com/javascript/jssamples/gp_clipasync.html

And you wont be able to access my two data layers unit and counties

I think the error message is referring to an error that occured in the ESRI API 3.9. Maybe because of the misuse of the API in my code.


Thank you for all the time spent on this. I really appreciate. I am learning a lot thanks to you and the other guys who helped me.
Alex
0 Kudos
AlexGole1
Emerging Contributor
0 Kudos