Uncaught TypeError: this.map.getLayer is not a function

4088
2
11-25-2016 06:22 AM
RolandoFlorez
New Contributor III

Hi, I need some of help

this is my code:

_reportLayerInfo : function () {
   console.warn('reportLayerInfo');
   console.log(this.allLayerInfos);
   var filteredArr = array.filter(this.allLayerInfos, function (item) {
      return item.getSubLayers().length == 0;
   });
   console.warn('filtered layers infos');
   console.log(filteredArr);
   array.forEach(filteredArr,function(layer){
      console.log(lang.hitch(this,this.map.getLayer(layer.id)));
   });
}

(sorry, I dont known how put a code block)

So, _reportLayerInfo() method is activate when I clic a button, now, when I clic the button, the console shows me that error: Uncaught TypeError: this.map.getLayer is not a function.

As you can see, I used the lang.hitch because I thought that It was the solution.

Thanks a lot.

0 Kudos
2 Replies
Drew
by
Occasional Contributor III

Try calling another function in your class;

Dojo Sample: (see processEvent)

require(["dojo/on", "dojo/_base/lang"], function(on, lang){

  var processEvent = function(e){
    this.something = "else";
  };

  on(something, "click", lang.hitch(this, processEvent));

});

So your code might be something like...

 array.forEach(filteredArr,function(layer){
      console.log(lang.hitch(this,getLayer);
   });

...
...
...



getLayer:function()
{
   this.map.getLayer(layer.id))
}‍‍‍‍‍‍‍‍‍‍‍
RobertScheitlin__GISP
MVP Emeritus

Rolando,


   I agree with Andrew that you need to use lang.hitch to keep your result functions in scope. So the main thing is anytime you have a function call after you do some process your results are likely in a different scope and you need to use lang.hitch to keep that function in the same scope as the main code.

0 Kudos