I am trying to figure out a good way to retrieve the fields from an ArcGIS REST service. I first create a FeatureLayer from the a feature server url. When I console.log the feature layer, I see that it has fields, but when I try to console.log(featureLayer.fields) it is just null.
I need the field information to create a popup template, but I am trying to find a way to avoid having to hard-code the field information. What is a good way of going about this?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<link rel="stylesheet" href="https://js.arcgis.com/4.13/esri/css/main.css">
<script src="https://js.arcgis.com/4.13/"></script>
</head>
<body>
<div id="viewDiv"></div>
<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/widgets/Legend",
"esri/PopupTemplate",
"esri/layers/FeatureLayer",
"esri/Graphic"
], function(Map, MapView, Legend, PopupTemplate, FeatureLayer, Graphic){
var map = new Map({
basemap: "gray"
});
var view = new MapView({
container: "viewDiv",
map: map
});
var featureLayer = new FeatureLayer({
url: "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/ArcGIS/rest/services/earthquakes_geojson/FeatureServer/0"
});
console.log(featureLayer);
console.log(featureLayer.fields);
// add the layer to the map
map.add(featureLayer);
});
</script>
</body>
</html>
Solved! Go to Solution.
I see, so your question is about the WAB and JS API 3.x? All the suggestions I've seen here are for the JS API 4.x because that is what the original question was about. You should consider creating a new question specific to your use-case as it's very different from this one.
Yes, we're running WAB and JS API 3.x. (deleted the comment above as I intended it to be a placeholder while trying to send you some code).
I see. My mistake. I thought that the structure of the FeatureLayer class follows the same data model across the version 3 and 4. Thanks.
And here's a quick CodePen using the service you mention above:
https://codepen.io/john-grayson/pen/MWowbgX