Select to view content in your preferred language

Add multiple layers error when using ESRI request interceptor to append security token

802
6
06-28-2023 08:56 AM
QingXu
by
New Contributor II

 I use ESRI request interceptors to append security token for secured services. Below is the basic code:

const interceptor = {

       urls: myUrlPatterns,

      before:   (params) => {
          const token = myMethodToGetToken();
         params.requestOptions.query = params.requestOptions.query || {};
        params.requestOptions.query.token = token;
      },

};

  esriConfig.request.interceptors.push(interceptor );
  esriConfig.request.useIdentity = true;

 

   This works basically fine.  However when I add multiple layers to the map  through map.addMany(layers) or layers.forEach(l => map.add(l)),  one of the layers can't be created due to  the error below:

    [esri.layers.FeatureLayer] #load() Failed to load layer (title: 'My layer title', id: 'my-layer-id') 

{error: RangeError: Maximum call stack size exceeded

      If I only add this layer by map.add(layer). it works.

    I looked into my console logs. When adding multiple layers, for example: layer0, layer1, layer2, layer3,  layer0 and layer2 won't be intercepted by ESRI request interceptor,  it seems these two layers will try to use identityManager to get the token.  But layer1 and layer 3 are intercepted by ESRI request interceptor immediately.  Either Layer0 or Layer2 will get the error above, and won't load.

    I tried to set useIdentity  to false, but layer0 and layer2 will get error saying token required.

   Any ideas on how to fix this?  

   Thanks!

    

 

  

0 Kudos
6 Replies
ReneRubalcava
Frequent Contributor II

Do you get the same error if you try adding your key to the customParameters of the layers? It should be available on all layer types. It was designed for a case like this where a token may not come from an ArcGIS identity workflow.

https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-MapImageLayer.html#customP...

0 Kudos
QingXu
by
New Contributor II

 I just tried to add token to customParameters,  but it still not works. And we'd like to use interceptor as the center place to handle resource security, adding customParameters compromises  this purpose..

  Thanks!

0 Kudos
JoelBennett
MVP Regular Contributor

The error you're receiving typically occurs for something like an infinite loop or infinite recursion.  If the problem is occurring somewhere within the code you've provided, the most likely place for it to happen would be in whatever processing "myMethodToGetToken" is doing.  Perhaps you could set up some breakpoints in that part of the workflow and ensure the code is doing what you intend.

0 Kudos
QingXu
by
New Contributor II

I already verified the error is not from my code. It is from ESRI code. 

 

RROR Error: Uncaught (in promise): RangeError: Maximum call stack size exceeded
RangeError: Maximum call stack size exceeded
at O (lang.js:5:1587)
at a (lang.js:5:522)
at a (lang.js:5:682)
at a (lang.js:5:682)
at a (lang.js:5:682)
at a (lang.js:5:682)
at a (lang.js:5:682)
at a (lang.js:5:682)
at a (lang.js:5:682)
at a (lang.js:5:682)
at resolvePromise (zone.js:1193:31)
at zone.js:1100:17
at zone.js:1116:33
at asyncGeneratorStep (asyncToGenerator.js:6:1)
at _throw (asyncToGenerator.js:25:1)
at _ZoneDelegate.invoke (zone.js:368:26)
at Object.onInvoke (core.mjs:26108:33)
at _ZoneDelegate.invoke (zone.js:367:52)
at Zone.run (zone.js:127:43)
at zone.js:1257:36

0 Kudos
ReneRubalcava
Frequent Contributor II

Are you using Angular? What version and what version of the API? There are some issues in older versions of the API and Zonejs in Angular. It looks like it might be a Zonejs issue.

https://github.com/Esri/jsapi-resources/tree/main/esm-samples/jsapi-angular-cli

0 Kudos
QingXu
by
New Contributor II

Yes, I am using Angular 16 and ArcGIS Javascript API 4.26.5, also tried latest ArcGIS API 4.27.6.   All other functions work, only this multiple layers having error. So  it seems it is not the angular zonejs issue.  The error trace may be misleading.  Thanks!

0 Kudos