Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

When working with the javascript API, I have been getting an error in FireBug when trying to execute the following script:

var featLayerURL = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/TaxParcel/AssessorsLiveLayers/MapServer/1";
var featLayer = new esri.layers.FeatureLayer(featLayerURL);
alert(featLayer.fields.length);

The error returned is "TypeError: featLayer.fields is undefined". At first I thought it was something wrong with one of my server's layers, but after using that sample server and getting the same error, I'm sure there is something else wrong. Here is a link to the REST response: Layer: Recorded Tax Parcel Sales (ID: 1). I have the feeling it is something really simple I'm forgetting, but for now it's been driving me crazy trying to figure it out.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

This is basically a timing issue. I recreated your sample and it appears that the response has not returned yet so when you immediately check the length you get the error. I moved the alert to its own function and called that function after the page had been initialized and everything worked as expected. If you want data other than the id then you will need to add an option to the constructor that returns all of the fields.

var featLayer = new esri.layers.FeatureLayer(featLayerURL, {
                    outFields: ["*"]
                });

Hope this helps.

share|improve this answer
    
Jamie is correct, the layer does not load instantly you should be listening to the event to check when it has loaded: aspect.after(layer, "onLoad", lang.hitch(this, function (addlayer) { alert(addlayer.fields.length); }), true); –  David Wilton Jan 9 '13 at 10:27
    
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.