gp result not getting passed to callback function

1579
3
06-27-2012 11:41 AM
JoanSteinbacher
New Contributor III
My javascript code is below. The geoprocessing task runs correctly and outputs a Table (not a feature class). Unfortunately the results aren't getting passed to the callback function.

   <script type="text/javascript">
      dojo.require("dijit.layout.BorderContainer");
      dojo.require("dijit.layout.ContentPane");
      dojo.require("dijit.Dialog");
      dojo.require("dojox.grid.DataGrid");
      dojo.require("dojo.data.ItemFileReadStore");

      dojo.require("esri.map");
      dojo.require("esri.tasks.gp");

      //Define global variables here.
      var map;
      var loading;
      var gp;
            
      function init() {
        loading = dojo.byId("loadingImg");
        var initExtent = new esri.geometry.Extent({"xmin":-9215700,"ymin":3216400,"xmax":-9119800,"ymax":3256100,"spatialReference":{"wkid":102100}});
        map = new esri.Map("map",{extent:initExtent});
  
      var imageParameters = new esri.layers.ImageParameters();
        imageParameters.format = "jpeg";  //set the image type to PNG24, note default is PNG8.
      
    //Cached Maps
       //Add the world street map layer to the map. View the ArcGIS Online site for services http://arcgisonline/home/search.html?t=content&f=typekeywords:service    
       var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
       map.addLayer(basemap); 
       var eluMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://gis.tpcmaps.org/ArcGIS/rest/services/LandUse/Existing_Land_Use/MapServer", {"opacity":0.5, "imageParameters":imageParameters});
       map.addLayer(eluMapServiceLayer); 
      var parcelMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://gis.tpcmaps.org/ArcGIS/rest/services/Parcels/MapServer", {"opacity":1.0, "imageParameters":imageParameters});
       map.addLayer(parcelMapServiceLayer);
    

      dojo.connect(map, 'onLoad', function(theMap) {
          //resize the map when the browser resizes
          dojo.connect(dijit.byId('map'), 'resize', map,map.resize);
        });
      }
      dojo.addOnLoad(init);
      
      function getLuLc() {
        //Geoprocessing Task 
        gp = new esri.tasks.Geoprocessor("http://gis.tpcmaps.org/ArcGIS/rest/services/ServerTools/GPServer/LuByFolioId");  
      
        console.log("inside getLuLc");
        console.log(dojo.byId("folio").value);
        //var params = { folioID: dojo.byId("folio").value };
        var params = { folioID: "191001.0000" };
        // SHOW LOADING DIALOG
        dijit.byId('loadingDialog').show();
        gp.submitJob(params, completeCallback, statusCallback);
        console.log("after submitJob call");
      }

      function statusCallback(jobInfo) {
        var status = jobInfo.jobStatus;
      }

      function completeCallback(jobInfo){
        console.log("inside completeCallback");
        var status = jobInfo.jobStatus;
        console.log(status);
         if(status === "esriJobFailed"){
           // HIDE LOADING DIALOG
           dijit.byId('loadingDialog').hide();
         }
         else if (status === "esriJobSucceeded"){
           console.log("success, jobId:" + jobInfo.jobId);
           // HIDE LOADING DIALOG
           dijit.byId('loadingDialog').hide();
           console.log("getting data");
          //gp.getResultData(jobInfo.jobId,"Output_Zip_File", downloadFile);
           gp.getResultData(jobInfo.jobId, "OutputTable", displayResult, displayError);
           console.log("after getting result data");
         }
      }

      function displayResult(results, messages) {
         console.log("inside displayResult");
         var resultFeatures = results.features;
         console.log("feature count: " + resultFeatures.length);
      }
      
      function displayError(results, messages) {
          alert("error in getResultData");
      }


The callback function displayResult is getting invoked, but the console gives the error that "resultFeatures" is undefined:

TypeError: resultFeatures is undefined
displayResult(undefined="[object Object]")MySample.html (line 98)
(undefined="[object Array]", undefined=""onGetResultDataComplete"", undefined="displayResult", undefined="[object Object]")?v=3.0 (line 34)
(undefined="[object Object]", undefined="[object Object]", undefined="displayResult", undefined="displayError", undefined="[object Object]")?v=3.0 (line 34)
(undefined="[object Object]", undefined="[object Object]", undefined="displayResult", undefined="displayError", undefined="[object Object]")?v=3.0 (line 15)
(undefined="[object Object]", undefined="[object Object]")?v=3.0 (line 34)
(undefined="[object Object]")


Do I have to output a feature class instead of a table for this to work? What am I missing?

Thanks in advance,
Joan
0 Kudos
3 Replies
KellyHutchins
Esri Frequent Contributor
I ran a quick test using your service and was able to retrieve the results successfully. Here's my test code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" />
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
    <title>GP Test</title>
    <link rel="stylesheet" href="https://community.esri.com//serverapi.arcgisonline.com/jsapi/arcgis/3.0/js/dgrid/css/dgrid.css">
    <link rel="stylesheet" href="https://community.esri.com//serverapi.arcgisonline.com/jsapi/arcgis/3.0/js/dgrid/css/skins/claro.css">
    <link rel="stylesheet" href="https://community.esri.com//serverapi.arcgisonline.com/jsapi/arcgis/3.0/js/dojo/dijit/themes/claro/claro.css">
    <style>
      html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
      .esriScalebar{
        padding: 20px 20px;
      }
      #map{
        padding:0;
      }
    </style>
    <script type="text/javascript">var djConfig = {parseOnLoad: true};</script>
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=3.0"></script>
 <script type="text/javascript">
      dojo.require("dijit.layout.BorderContainer");
      dojo.require("dijit.layout.ContentPane");
      dojo.require("dgrid.Grid");

      dojo.require("esri.map");
      dojo.require("esri.tasks.gp");

      //Define global variables here.
      var map;
      var gp;
      var grid;
            
      function init() {
        loading = dojo.byId("loadingImg");
        var initExtent = new esri.geometry.Extent({"xmin":-9215700,"ymin":3216400,"xmax":-9119800,"ymax":3256100,"spatialReference":{"wkid":102100}});
        map = new esri.Map("map",{extent:initExtent});
  
      var imageParameters = new esri.layers.ImageParameters();
        imageParameters.format = "jpeg";  //set the image type to PNG24, note default is PNG8.
      
    //Cached Maps
       //Add the world street map layer to the map. View the ArcGIS Online site for services http://arcgisonline/home/search.html?t=content&f=typekeywords:service    
       var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer");
       map.addLayer(basemap); 
       var eluMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://gis.tpcmaps.org/ArcGIS/rest/services/LandUse/Existing_Land_Use/MapServer", {"opacity":0.5, "imageParameters":imageParameters});
       map.addLayer(eluMapServiceLayer); 
      var parcelMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://gis.tpcmaps.org/ArcGIS/rest/services/Parcels/MapServer", {"opacity":1.0, "imageParameters":imageParameters});
       map.addLayer(parcelMapServiceLayer);
    

      dojo.connect(map, 'onLoad', function(theMap) {
          //resize the map when the browser resizes
          dojo.connect(dijit.byId('map'), 'resize', map,map.resize);
        });
      }
      dojo.addOnLoad(init);
      
      function getLuLc() {
        //Geoprocessing Task 
        gp = new esri.tasks.Geoprocessor("http://gis.tpcmaps.org/ArcGIS/rest/services/ServerTools/GPServer/LuByFolioId");  
      
        //var params = { folioID: dojo.byId("folio").value };
        var params = { folioID: "191001.0000" };

        gp.submitJob(params, completeCallback, statusCallback);
  
      }

      function statusCallback(jobInfo) {
        var status = jobInfo.jobStatus;
        dojo.byId('status').innerHTML = status;
      }

      function completeCallback(jobInfo){
        
        var status = jobInfo.jobStatus;
        
         if (status === "esriJobSucceeded"){
            console.log("success, jobId:" + jobInfo.jobId);
           gp.getResultData(jobInfo.jobId, "OutputTable", displayResult, displayError);

         }
      }

      function displayResult(results, messages) {
        //clear the status messages 
        dojo.byId('status').innerHTML = '';
         var resultFeatures = results.value;
         //display the results in a dgrid 
         var data = [];
         if(grid){
            grid.refresh();
         }
         data = dojo.map(resultFeatures.features,function(feature){
            return {
                'FOLIO':feature.attributes.FOLIO,
                'SUM_ACREAGE': feature.attributes.SUM_ACREAGE,
                'FLUE': feature.attributes.FLUE,
                'ELUSHADE':feature.attributes.ELUSHADE
            }
         });
        grid = new dgrid.Grid({
            columns:{
                'FOLIO': 'Folio',
                'SUM_ACREAGE': 'Acreage',
                'FLUE': 'Flue',
                'ELUSHADE': 'SF'
            }
         },'grid');
         
         grid.renderArray(data);
         
         
      }
      
      function displayError(results, messages) {
          alert("error in getResultData");
      }
      </script>
      
        </head>
  
  <body class="claro">
    <div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false" style="width: 100%; height: 100%; margin: 0;">
      <div id="map" dojotype="dijit.layout.ContentPane" region="center" style="border:1px solid #000;padding:0;">
     </div>
     <div id='rightPane' dojotype='dijit.layout.ContentPane' region='right' style='width:400px;'>
        <input type='button' onclick="getLuLc();" value ='Execute Task'/>
        <div id='status'></div>
        <div id='grid'></div>
     </div>
    </div>
  </body>

</html>
0 Kudos
JoanSteinbacher
New Contributor III
Kelly,

After looking at your code I see where I went wrong when trying to access the feature set that was passed in. To summarize, what is getting passed in is an object and you have to get the value of the object (in this case a feature set) before you can access the features in the feature set.

//My line:
var resultFeatures = results.features;

//Your line:
var resultFeatures = results.value;


I appreciate you taking the time to clear this up for me.

Many thanks,
Joan
0 Kudos
GUSTAVOMOLLERI
New Contributor
I am having almost the same problem. But with a Feature Class.

The result of my gp task was ok at the server and also runs fine when I call it from the script. The problem is that it is not displayed at my app.

I really cound not find the scritp error. And I spent a week trying to find it.

HELP!

Here is my code:


   
    dojo.require("esri.layers.FeatureLayer");
    dojo.require("esri.map");
    //dojo.require("dijit.TooltipDialog");
    //dojo.require("esri.dijit.InfoWindowLite");
    dojo.require("dojo.number");
    //dojo.require("dojox.grid.DataGrid");
    //dojo.require("dojo.data.ItemFileReadStore");
    //dojo.require("dijit.form.TextBox");
    //dojo.require("dojox.grid.DataGrid");
    //dojo.require("dojo.data.ItemFileReadStore");
   // dojo.require("dojo.parser");
    dojo.require("dijit.Menu");
    dojo.require("esri.dijit.BasemapGallery");
    dojo.require("esri.tasks.geometry");
    dojo.require("esri.tasks.gp");
    dojo.require("esri.graphic");
    dojo.require("esri.layers.graphics");
    //dojo.require("esri.tasks.FeatureSet");
    //-------------------------------------------
   
   
    // Dados base
    var map;    
   
    // Layers
    var hidro;
   
    // Tools
    var gp;
   
     
    // FUNCOES
    function init() {
   
     // Extensao de visualizacao
    
     var spatialRef = new esri.SpatialReference({wkid:4618});
     var startExtent = new esri.geometry.Extent();
     startExtent.xmin = -44;
     startExtent.ymin = -17;
     startExtent.xmax = -45;
     startExtent.ymax = -13;
     startExtent.spatialReference = spatialRef;
   
   
     // Cria o MAPA
     map = new esri.Map("map", { extent:esri.geometry.geographicToWebMercator(startExtent),logo: false});
          
    
     // Indica o caminha dos LAYERS
     // MAPA BASE
    
     // IBGE http://mapasinterativos.ibge.gov.br/ArcGIS/rest/services/GEOLOGIA/MapServer
    
     // Imagens aereas
     var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer");
     map.addLayer(basemap);  
    
     // Toponimias e divisas
     //var basemap2 = new esri.layers.ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/Reference/World_Boundaries_and_Places/MapServe...");
     //map.addLayer(basemap2);          
                   
       
        // HIDROGRAFIA
        var content = "<br><b>Curso d'agua</b>: ${HIN_CD_CURSO_DAGUA}" +  
      "<br><b>Cod. Bacia</b>: ${HIN_CD_OTTOBACIA} " +
      "<br><b>Área cont. (km)</b>: ${HIN_AR_CONTRIBUICAO} " +
            "<br><b>Area montante (km)</b>: ${HIN_AR_MONTANTE} "
    
     template = new esri.InfoTemplate();
     template.setTitle("<b>Nome do Rio</b>: ${HIN_NM_COMPLETO}");
     //template.setContent(getTextContent);
     template.setContent(content);  
        hidro = new esri.layers.FeatureLayer("http://10.8.10.50/ArcGIS/rest/services/area_mon/MapServer/0",{mode:esri.layers.FeatureLayer.MODE_ONDEMAND});
   
             
        //hidro = new esri.layers.FeatureLayer("http://10.8.10.50/ArcGIS/rest/services/area_mon/MapServer/0",{mode:esri.layers.FeatureLayer.MODE_ONDEMAND, infoTemplate: template, outFields: ["*"], "id":"hidrografia"});
         
        hidro.setScaleRange(0,0);                      
        hidro.setDefinitionExpression("HIN_DS_DOMINIALIDADE='1'");
        //map.addLayer(hidro);
       
        reg = new esri.layers.FeatureLayer("http://10.8.10.50/ArcGIS/rest/services/area_mon/MapServer/1",{mode:esri.layers.FeatureLayer.MODE_ONDEMAND});
        map.addLayer(reg);
       
        // TOOLS
        gp = new esri.tasks.Geoprocessor("http://10.8.10.50/ArcGIS/rest/services/ANA-GEGEO-Tools_3/GPServer/Area_mon_Rios_Fed_v2");
        //gp.setOutputSpatialReference({wkid:4618});           
       
       
       
        //------
        //map.infoWindow.resize(465,440);            
   
       
        dojo.connect(map, 'onLoad', function(map) {
          //resize the map when the browser resizes
          dojo.connect(dijit.byId('map'), 'resize', map,map.resize);
        });
    }  
   
   
   
   
    // FUNCOES ESPECIFICAS  
    // Roda tool
    function tool_area_mon(cod,area) {

       
        codigo = cod.value.toString();
        areaMon = area.value.toString();
       
        var params = {"Codigo_Otto":codigo, "Area_Montante":areaMon};
   
        alert("Iniciando processamento! Aguarde alguns seg. "+codigo+" "+areaMon);
   
        gp.submitJob(params, completeCallback , statusCallback, function(error){
            alert(error);
        });
   
    }



    function completeCallback(jobInfo){
               
        if(jobInfo.jobStatus !== "esriJobFailed"){
       
            gp.getResultData(jobInfo.jobId,"Arq_Saida", displayResult);
           
        }
       
    }
   


    function displayResult(result, messages) {
       
        alert("inicio");
        var simpleLineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,0,0.5]), 3);
        var features = result.value.features;
       
        var gpResult = new esri.layers.GraphicsLayer({id: "Result"});
       
        for (var f = 0, fl = features.length; f < fl; f++) {
            var feature = features;
            feature.setSymbol(simpleLineSymbol);
            gpResult.add(feature);       
        };
       
        map.addLayer(gpResult);
        alert("fim");
       
    } 
   
   
   
    function statusCallback(jobInfo) {
        alert(jobInfo.jobStatus)
    //            var status = jobInfo.jobStatus;
    //            if(status === "esriJobFailed"){
    //                alert(status);
    //                esri.hide(loading);
    //            }
    //           
    //            else if (status === "esriJobSucceeded"){
    //              esri.hide(loading);
    //            }
       
    }    
   
    dojo.addOnLoad(init);
0 Kudos