title: leaflet Uncaught TypeError: map.addLayer is not a function
body: <p>I've made a function for clustering my json data on map like this:
  it's located in a js file called <code>functions.js</code></p>
  <pre><code>function(ODO)    
  var markerClusters = L.markerClusterGroup();
      for ( var i = 0; i &lt; ODO.length ; i++ )
      {
          var popup = ODO[i].branchcode +
                  &quot;&lt;br/&gt;&lt;b&gt;کد رشته :&lt;/b&gt; &quot; +
                  ODO[i].branchcode +
                  &quot;&lt;br/&gt;&lt;b&gt;دوره:&lt;/b&gt; &quot; +
                  ODO[i].saleprd +
                  &quot;&lt;br/&gt;&lt;b&gt;سال :&lt;/b&gt; &quot; +
                  ODO[i].saleyear +
                  &quot;&lt;br/&gt;&lt;b&gt;موقعیت مکانی:&lt;/b&gt; &quot; +
                  ODO[i].LastX +
                  &quot;&quot; +
                  &quot;-&quot; +
                  &quot;&quot; +
                  ODO[i].LastY +
                  &quot;&lt;br/&gt;&lt;b&gt;فلگ :&lt;/b&gt; &quot; +
                  ODO[i].flag;
          var mainMap = L.marker( [ODO[i].LastX, ODO[i].LastY] )
                          .bindPopup( popup );
          markerClusters.addLayer( mainMap );
      }
      
      mainMap.addLayer( markerClusters );
      markerClusterLayer = L.markerClusterGroup({
          disableClusteringAtZoom: 13
      }).addTo(mainMap);
  }
  </code></pre>
  <p>and then, initialized a map with defferent layers and a legend in <code>app.js</code>:</p>
  <pre><code>function Init_Map()
  {
      var cities = L.layerGroup();
    
      var mbAttr = 'Map data &amp;copy; &lt;a href=&quot;https://www.openstreetmap.org/&quot;&gt;OpenStreetMap&lt;/a&gt; contributors, ' +
          '&lt;a href=&quot;https://creativecommons.org/licenses/by-sa/2.0/&quot;&gt;CC-BY-SA&lt;/a&gt;, ' +
          'Imagery © &lt;a href=&quot;https://www.mapbox.com/&quot;&gt;Mapbox&lt;/a&gt;',
        mbUrl = 'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw';
    
      var satellite   = L.tileLayer(mbUrl, {id: 'mapbox/satellite-v9', tileSize: 512, zoomOffset: -1, attribution: mbAttr}),
          streets  = L.tileLayer(mbUrl, {id: 'mapbox/streets-v11', tileSize: 512, zoomOffset: -1, attribution: mbAttr}),
          Defaultmap = L.tileLayer('http://mt0.google.com/vt/lyrs=p&amp;hl=en&amp;x={x}&amp;y={y}&amp;z={z}&amp;s=Ga', {
              maxZoom: 19,
              attribution: '&amp;copy; &lt;a href=&quot;https://openstreetmap.org/copyright&quot;&gt;OpenStreetMap contributors&lt;/a&gt;'}),
          googleHybrid = L.tileLayer('http://{s}.google.com/vt/lyrs=s,h&amp;x={x}&amp;y={y}&amp;z={z}',{
              maxZoom: 20,
              subdomains:['mt0','mt1','mt2','mt3']}),
          googleSat = L.tileLayer('http://{s}.google.com/vt/lyrs=s&amp;x={x}&amp;y={y}&amp;z={z}',{
              maxZoom: 20,
              subdomains:['mt0','mt1','mt2','mt3']}),
          googleTerrain = L.tileLayer('http://{s}.google.com/vt/lyrs=p&amp;x={x}&amp;y={y}&amp;z={z}',{
              maxZoom: 20,
              subdomains:['mt0','mt1','mt2','mt3']});
  
      window.mainMap = L.map('map', {
        center: [36.564012,53.060300],
        zoom: 8,
        layers: [satellite, cities, Defaultmap , googleHybrid , googleSat , googleTerrain]
      });
      var baseLayers = {
        &quot;نقشه ماهواره ای(گوگل)&quot;: googleSat,
        &quot;نقشه هیبریدی&quot;: googleHybrid,
        &quot;نقشه ماهواره ای(OSM)&quot;: satellite,
        &quot;Streets&quot;: streets,
        &quot;نقشه اصلی&quot;: Defaultmap,
        &quot;نقشه Terrain&quot;: googleTerrain
      };
    
      var overlays = {
        &quot;Cities&quot;: cities
      };
    
      L.control.layers(baseLayers, overlays).addTo(mainMap);
      L.Control.Consumption = L.Control.extend({
          onAdd: function(map) {
              var div = L.DomUtil.create(&quot;div&quot;, &quot;legend&quot;);
              div.innerHTML += &quot;&lt;h4&gt;Tegnforklaring&lt;/h4&gt;&quot;;
              div.innerHTML +=
              '&lt;i style=&quot;background: rgba(110, 204, 57, 0.6)&quot;&gt;&lt;/i&gt;&lt;span&gt;خوش مصرف&lt;/span&gt;&lt;br&gt;';
              div.innerHTML +=
              '&lt;i style=&quot;background: rgba(240, 194, 12, 0.6)&quot;&gt;&lt;/i&gt;&lt;span&gt;کم مصرف&lt;/span&gt;&lt;br&gt;';
              div.innerHTML +=
              '&lt;i style=&quot;background: rgba(241, 128, 23, 0.6)&quot;&gt;&lt;/i&gt;&lt;span&gt;پر مصرف&lt;/span&gt;&lt;br&gt;';
              return div;
          },
          onRemove: function(map) {}
      });
      
      L.control.consumption = function(opts) {
          return new L.Control.Consumption(opts);
      }
      L.control.consumption({ position: 'bottomright' }).addTo(mainMap);
  }
  </code></pre>
  <p>and I'm calling data from server like this:</p>
  <pre><code>var OMID_Detail_Object;
  function OMID_Detail() {
  
    var Ajax_URL= Server_IP +'/OHM';
    var Year_Val = GetSelectValue(&quot;YearSelect&quot;);
    var Prd_Val = GetSelectValue(&quot;PrdSelect&quot;);
    var Flg_Val = GetSelectValue(&quot;flags&quot;);
  
    
    app.request.get(Ajax_URL, { &quot;Token&quot;: Token_Data, &quot;SaleYear&quot;:Year_Val, &quot;SalePrd&quot;:Prd_Val, &quot;Flag&quot;:Flg_Val }, function (data)
    {
      OMID_Detail_Object=data;
      MarkerOnMap(OMID_Detail_Object);
    },function (er){},&quot;json&quot;);
  }
  function CreateMarkers(){
    OMID_Detail();
  }
  </code></pre>
  <p>I named the default map <code>mainMap</code> when I encountered an error for legend. but now, the function for clustering markers have an error like:</p>
  <pre><code>Uncaught TypeError: mainMap.addLayer is not a function
  at MarkerOnMap (functions.js:46)
  at Object.success (app.js:213)
  at i (0:4002)
  at XMLHttpRequest.Request.f.onload (0:4179)
  </code></pre>
  <p>and is talking about an error in this part:</p>
  <pre><code>    mainMap.addLayer( markerClusters );
  </code></pre>
  <p>but why? I tried saying</p>
  <pre><code>    map.addLayer( markerClusters );
  </code></pre>
  <p>but it didn't work,too. had the same error!
  can anyone help with this?</p>