1

I'm just a simple farmer tending to his ArcGIS online hosted feature layers. I pinpointed the problem as that I can't seem to properly create a FeatureLayer object based on a url to a hosted feature layer. I can execute query tasks and what not just fine, but I'm really not sure where I'm going wrong with creating a feature layer. I tried just using an esri example feature layer, and it's still not working for me. Am I just missing something dumb? Javascript API v3.31 btw

My code:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>testfeatures</title>
    <script src="https://js.arcgis.com/3.31/"></script>
    <script>
      require([
        "dojo/_base/declare",
        "dojo/dom",
        "dojo/parser",
        "dojo/ready",
        "esri/layers/FeatureLayer"
    ], function(declare, dom, parser, ready, FeatureLayer) {
        parser.parse();

        ready(function() {
            var testLayer = new FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0", {
                mode: FeatureLayer.MODE_AUTO,
                outFields: ["*"]
            });

            console.log(testLayer);
            console.log(testLayer.fields)
        })
    });
    </script>
</head>
<body>

</body>

Console logging the fields gives "undefined", whereas logging the feature layer itself gives me an object.
Thanks for any help!

1

Don't worry you are doing OK, it's just that the layer has not load completely, check this example,

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>FeatureLayer</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.31/esri/css/esri.css">
<script src="https://js.arcgis.com/3.31/"></script>
<style>
html, body, #map {
  padding: 0;
  margin: 0;
  height: 100%;
  width: 100%;
}
</style>
<script>
require([
    "esri/map",
    "esri/layers/FeatureLayer",
    "dojo/domReady!"
  ],
  function(
    Map,
    FeatureLayer
  ) {
    var map = new Map("map", {
      basemap: "hybrid",
      center: [-82.44109, 35.6122],
      zoom: 6
    });
    var featureLayer = new FeatureLayer(
      "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0",
      {
        mode: FeatureLayer.MODE_AUTO,
        outFields: ["*"]
      }
    );
    setTimeout(function() {
      console.log(featureLayer.fields);
    }, 1000);
    console.log(featureLayer);
    map.addLayer(featureLayer);
  });
</script>
</head>
<body>
  <div id="map"></div>
</body>
</html>

There you will see the result of featureLayer.fields.

Also, there was a similar old question about this GIS SE - featureLayer.fields is undefined

2
  • 1
    I see! Alright, that makes total sense, I just hadn't though about the feature layer itself not loading yet. Hadn't come across the older post. Many thanks!
    – Nick
    Mar 18 '20 at 18:14
  • Glad it helps you!
    – cabesuon
    Mar 18 '20 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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