Select to view content in your preferred language

query a feature layer and the onfault function is being called

1596
12
09-14-2011 06:04 AM
PeterHoffman
Deactivated User
I am trying to query a feature layer and the onfault function is being called. The first query works fine but the second one - // Status part does not.

Any ideas?

Thanks.



<?xml version="1.0" encoding="utf-8"?>
<!--
     ////////////////////////////////////////////////////////////////////////////////
     //
     // Copyright (c) 2010 ESRI
     //
     // All rights reserved under the copyright laws of the United States.
     // You may freely redistribute and use this software, with or
     // without modification, provided you include the original copyright
     // and use restrictions.  See use restrictions in the file:
     // <install location>/License.txt
     //
     ////////////////////////////////////////////////////////////////////////////////
-->
<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   xmlns:viewer="com.esri.viewer.*"
                   widgetConfigLoaded="init()">
    <fx:Script>
        <![CDATA[
   import com.esri.ags.FeatureSet;
   import com.esri.ags.layers.FeatureLayer;
   import com.esri.ags.tasks.supportClasses.Query;
   import com.esri.viewer.components.HTMLPopup;
  
   import flashx.textLayout.edit.SelectionFormat;
   import flashx.textLayout.events.SelectionEvent;
  
   import mx.collections.ArrayCollection;
   import mx.collections.SortField;
   import mx.controls.Alert;
   import mx.rpc.AsyncResponder;
   import mx.rpc.events.FaultEvent;

            private var content:String;
   private var contentStatus:String;
            private var btnLabel:String
            private var txtareaWidth:Number;
            private var txtareaHeight:Number;
   private var sSheltersClosedMessage:String;
   private var sSheltersOpenMessage:String;
  
  
   private var fLayer:FeatureLayer
   private var statusLayer:FeatureLayer
  
            private function init():void
            {
                if (configXML)
                {
                    content = configXML.content || "...";
                    btnLabel = configXML.btnlabel || getDefaultString("okLabel");
                    txtareaWidth = configXML.width || 500;
                    txtareaHeight = configXML.height || 500;
     sSheltersClosedMessage = configXML.sheltersclosedmessage;
     sSheltersOpenMessage = configXML.sheltersopenmessage;
    
     fLayer = new FeatureLayer(configXML.sheltersurl);
     fLayer.useAMF = false;
    
     var qry:Query = new Query();
     qry.where = "OPEN = 1";
     // fLayer.queryFeatures(qry, new AsyncResponder(onResult, onFault));
    
     // Status part
     statusLayer = new FeatureLayer("http://gis.co.suffolk.ny.us/ArcGIS/rest/services/EmergencyShelters/MapServer/1");  
     fLayer.useAMF = false;
    
     var qryStatus:Query = new Query();
     qryStatus.where = "STATUS = 1";
     statusLayer.queryFeatures(qryStatus, new AsyncResponder(onResultStatus, onFault));   
    
                }
       
    function onResultStatus(fSet:FeatureSet, token:Object=null):void
    {
     trace("Result");
     if(fSet.features.length > 0)
     {
      contentStatus = "HERE1";
     
      HTMLPopup.show(contentStatus, btnLabel, txtareaWidth, txtareaHeight);
     }
     else
     {
      contentStatus = "HERE";
      HTMLPopup.show(contentStatus, btnLabel, txtareaWidth, txtareaHeight);
     
     }
    }
       
    function onResult(fSet:FeatureSet, token:Object=null):void
    {
     trace("Result");
    
     if(fSet.features.length > 0)
     {
      content = content.replace("[SHELTERMESSAGETOKEN]", sSheltersOpenMessage);
     
     
      var arr:Array = new Array();
      for(var i:int=0; i<fSet.features.length; i++)
      {
       arr.push(fSet.attributes.NAME);
      }
      arr.sort();
     
     
      var tmp:String = "";
      for(i=0; i<arr.length; i++)
      {
       tmp += arr + "<br>";
      }
      tmp = tmp += "<br>";
     
     
      content = content.replace("[OPENSHELTERSTOKEN]", tmp);
      var bkgrndColor:String = "0x00FF00";
      HTMLPopup.show(content, btnLabel, txtareaWidth, txtareaHeight);
     }
     else
     {
      content = content.replace("[SHELTERMESSAGETOKEN]", sSheltersClosedMessage);
      HTMLPopup.show(content, btnLabel, txtareaWidth, txtareaHeight);
    
     }
    }
   
    function onFault(info:Object, token:Object = null):void
    {
     trace("Fault");
    }
            }
        ]]>
    </fx:Script>
</viewer:BaseWidget>
Tags (2)
0 Kudos
12 Replies
RobertScheitlin__GISP
MVP Emeritus
Peter,

   The likely issue is that the feature layer is not yet loaded when you are attempting to run the query. You need to ensure the layer is loaded first.

<?xml version="1.0" encoding="utf-8"?>
<!--
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2010 ESRI
//
// All rights reserved under the copyright laws of the United States.
// You may freely redistribute and use this software, with or
// without modification, provided you include the original copyright
// and use restrictions. See use restrictions in the file:
// <install location>/License.txt
//
////////////////////////////////////////////////////////////////////////////////
-->
<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
                   xmlns:s="library://ns.adobe.com/flex/spark"
                   xmlns:mx="library://ns.adobe.com/flex/mx"
                   xmlns:viewer="com.esri.viewer.*"
                   widgetConfigLoaded="init()">
    <fx:Script>
        <![CDATA[
            import com.esri.ags.FeatureSet;
            import com.esri.ags.events.LayerEvent;
            import com.esri.ags.layers.FeatureLayer;
            import com.esri.ags.tasks.supportClasses.Query;
            import com.esri.viewer.components.HTMLPopup;
            
            import flashx.textLayout.edit.SelectionFormat;
            import flashx.textLayout.events.SelectionEvent;
            
            import mx.collections.ArrayCollection;
            import mx.collections.SortField;
            import mx.controls.Alert;
            import mx.rpc.AsyncResponder;
            import mx.rpc.events.FaultEvent;
            
            private var content:String;
            private var contentStatus:String;
            private var btnLabel:String
            private var txtareaWidth:Number;
            private var txtareaHeight:Number;
            private var sSheltersClosedMessage:String;
            private var sSheltersOpenMessage:String;
            
            
            private var fLayer:FeatureLayer
            private var statusLayer:FeatureLayer
            
            private function init():void
            {
                if (configXML)
                {
                    content = configXML.content || "...";
                    btnLabel = configXML.btnlabel || getDefaultString("okLabel");
                    txtareaWidth = configXML.width || 500;
                    txtareaHeight = configXML.height || 500;
                    sSheltersClosedMessage = configXML.sheltersclosedmessage;
                    sSheltersOpenMessage = configXML.sheltersopenmessage;
                    
                    fLayer = new FeatureLayer(configXML.sheltersurl);
                    fLayer.useAMF = false;
                    
                    var qry:Query = new Query();
                    qry.where = "OPEN = 1";
                    // fLayer.queryFeatures(qry, new AsyncResponder(onResult, onFault));
                    
                    // Status part
                    statusLayer = new FeatureLayer("http://gis.co.suffolk.ny.us/ArcGIS/rest/services/EmergencyShelters/MapServer/1");
                    fLayer.useAMF = false;
                    statusLayer.addEventListener(LayerEvent.LOAD, execQuery);
                    
                }
                
                private function execQuery(evt:Event):void
                {
                    var qryStatus:Query = new Query();
                    qryStatus.where = "STATUS = 1";
                    statusLayer.queryFeatures(qryStatus, new AsyncResponder(onResultStatus, onFault));
                }
                
                private function onResultStatus(fSet:FeatureSet, token:Object=null):void
                {
                    trace("Result");
                    if(fSet.features.length > 0)
                    {
                        contentStatus = "HERE1";
                        
                        HTMLPopup.show(contentStatus, btnLabel, txtareaWidth, txtareaHeight);
                    }
                    else
                    {
                        contentStatus = "HERE";
                        HTMLPopup.show(contentStatus, btnLabel, txtareaWidth, txtareaHeight);
                        
                    }
                }
                
                private function onResult(fSet:FeatureSet, token:Object=null):void
                {
                    trace("Result");
                    
                    if(fSet.features.length > 0)
                    {
                        content = content.replace("[SHELTERMESSAGETOKEN]", sSheltersOpenMessage);
                        
                        
                        var arr:Array = new Array();
                        for(var i:int=0; i<fSet.features.length; i++)
                        {
                            arr.push(fSet.attributes.NAME);
                        }
                        arr.sort();
                        
                        
                        var tmp:String = "";
                        for(i=0; i<arr.length; i++)
                        {
                            tmp += arr + "<br>";
                        }
                        tmp = tmp += "<br>";
                        
                        
                        content = content.replace("[OPENSHELTERSTOKEN]", tmp);
                        var bkgrndColor:String = "0x00FF00";
                        HTMLPopup.show(content, btnLabel, txtareaWidth, txtareaHeight);
                    }
                    else
                    {
                        content = content.replace("[SHELTERMESSAGETOKEN]", sSheltersClosedMessage);
                        HTMLPopup.show(content, btnLabel, txtareaWidth, txtareaHeight);
                        
                    }
                }
                
                private function onFault(info:Object, token:Object = null):void
                {
                    trace("Fault");
                }
            }
        ]]>
    </fx:Script>
</viewer:BaseWidget> 
0 Kudos
PeterHoffman
Deactivated User
The first query works and uses a feature layer that is located in the config.xml file for this code.
The second query has the url hard coded and does not work.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Peter,

   Initially I did not check your url. Upon checking though I see you are using the wrong url.

http://gis.co.suffolk.ny.us/ArcGIS/rest/services/EmergencyShelters/MapServer/0
0 Kudos
PeterHoffman
Deactivated User
Our department has web filtering in place and posting to this or any other website doesn't work to well.

To clarify:
Layer "0" is for the first query.

Layer "1" is for the 2nd query.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Peter,

   Hmm...

http://gis.co.suffolk.ny.us/ArcGIS/rest/services/EmergencyShelters/MapServer/1

is the IMS.SHELTER SYSTEM and does not have an open field like you are specifying in this line of code:

qry.where = "OPEN = 1";
http://gis.co.suffolk.ny.us/ArcGIS/rest/services/EmergencyShelters/MapServer/0

does have an OPEN field...
0 Kudos
PeterHoffman
Deactivated User
I am looking for this in layer 1

qryStatus.where = "STATUS = 1";
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Peter,

it looks like there is something wrong with this layer.

http://gis.co.suffolk.ny.us/ArcGIS/rest/services/EmergencyShelters/MapServer/1

Extent: 
 XMin: NaN
YMin: NaN
XMax: NaN
YMax: NaN
Spatial Reference: 2263


It should not have those NaNs there for the extent.
0 Kudos
PeterHoffman
Deactivated User
It does not have extents because it is a related table.
I can run a query in rest web site such as
where status = 1 and it returns correctly:

# results: 1

1
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Peter,

   That is the issue than you are attempting to load that url as a featurelayer and then query it but it has no feature because it is a flat table. You need to use a QueryTask instead.
0 Kudos