AnsweredAssumed Answered

Get fields from featureLayer created from rest service?

Question asked by vlantaca on Dec 4, 2019
Latest reply on Dec 4, 2019 by kenbuja

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>

Outcomes