I am creating a browser app that integrates the ArcGIS API for Javascript, and ArcGIS Rest JS in order to allow users to create and edit Feature Layers hosted on ArcGIS Online. I am able to create a hosted Feature Layer that appears in my 'Contents' on ArcGIS Online, but I am unable to add this layer to my WebMap. Here is the relevant function below, called by a widget when clicked:
<SPAN class="keyword token">function</SPAN> <SPAN class="token function">createSite</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="comment token">// pass credentials of auth session from JS API to Rest JS</SPAN>
esriId<SPAN class="punctuation token">.</SPAN><SPAN class="token function">getCredential</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"https://arcgis.com/sharing/rest/"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="token function">then</SPAN><SPAN class="punctuation token">(</SPAN>cred <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">const</SPAN> session <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">arcgisRest<SPAN class="punctuation token">.</SPAN>UserSession<SPAN class="punctuation token">.</SPAN>fromCredential</SPAN><SPAN class="punctuation token">(</SPAN>cred<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">// create the item</SPAN>
arcgisRest<SPAN class="punctuation token">.</SPAN><SPAN class="token function">createFeatureService</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN>
item<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN>
name<SPAN class="punctuation token">:</SPAN> sitename<SPAN class="punctuation token">,</SPAN>
capabilities<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Create, Delete, Query, Update"</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">,</SPAN>
authentication<SPAN class="punctuation token">:</SPAN> session
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">//get url of newly created feature service</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="token function">then</SPAN><SPAN class="punctuation token">(</SPAN>response <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN><SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> newURL <SPAN class="operator token">=</SPAN> response<SPAN class="punctuation token">.</SPAN>encodedServiceURL<SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">// add service definition</SPAN>
arcgisRest<SPAN class="punctuation token">.</SPAN><SPAN class="token function">addToServiceDefinition</SPAN><SPAN class="punctuation token">(</SPAN>newURL<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">{</SPAN>
authentication<SPAN class="punctuation token">:</SPAN> session<SPAN class="punctuation token">,</SPAN>
layers<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN> <SPAN class="punctuation token">{</SPAN>name<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"thisLayer"</SPAN><SPAN class="punctuation token">,</SPAN>
type<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"Feature Layer"</SPAN><SPAN class="punctuation token">,</SPAN>
geometryType<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"esriGeometryPoint"</SPAN><SPAN class="punctuation token">,</SPAN>
fields<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN>
<SPAN class="punctuation token">{</SPAN>
name<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"FID"</SPAN><SPAN class="punctuation token">,</SPAN>
type<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"esriFieldTypeInteger"</SPAN><SPAN class="punctuation token">,</SPAN>
actualType<SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"int"</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="comment token">// refresh layers on map view - remove all, query, and load again</SPAN>
map<SPAN class="punctuation token">.</SPAN>layers<SPAN class="punctuation token">.</SPAN><SPAN class="token function">removeAll</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="token function">loadLayers</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Again, this successfully creates a Feature Layer in my ArcGIS Online Contents. However, when loadLayers() is called - which successfully loads the other Feature Layers from my account into the WebMap - the new layer does not appear. Likewise, when I try to open this new layer directly in the 'Map Viewer' on ArcGIS Online, nothing happens and the layer does not appear in the contents pane of the map.
Is this a problem with the layer details of the new Feature Layer? In other words, are there parameters and/or fields that I need to include in the addToServiceDefinition call in order for the newly created Feature Layer to have a valid format? Or is it possible to specify at creation that this new Feature Layer should mimic a template, such as the 'Points' template that is available when creating Feature Layers manually on ArcGIS Online?
In short, I need the new Feature Layer I create to be valid and able to display and edit in the WebMap viewer. Any help with this would be greatly appreciated. Thanks.