Ошибка переменной «не определено» функция updateMap (), хотя определена как глобальная переменная в другом месте скрипта - PullRequest
0 голосов
/ 01 мая 2020

Я работаю над картой листовки, и продолжаю получать ошибку Uncaught ReferenceError: colorize не определен. Переменная четко определена и является глобальной по объему. Когда я пытаюсь объявить colorize локально с другим именем переменной, я получаю ту же ошибку. Заставляет меня думать, что это ошибка внутри самой функции.

function processData(counties, data) {

        // loop through all the counties
        for (let i of counties.features) {

          // for each of the CSV data rows
          for (let j of data.data) {

            // if the county FIPS code and data FIPS code match
            if (i.properties.COUNTYFP === j.COUNTY_FIP) {

              // console.log the values
              //            console.log('county fip: ', i.properties.COUNTYFP );
              //            console.log('data fip: ', j.COUNTY_FIP);

              // re-assign the data for that county as the county's props
              i.properties.unemploymentData = j;

              // break from inner loop
              break;
            }
            //  console.log('after: ', counties);
          }
          // // empty array to store all the data values
          const rates = [];

          // iterate through all the counties
          counties.features.forEach(function (county) {

            // iterate through all the props of each county
            for (const prop in county.properties) {

              //     // if the attribute is a number and not one of the fips codes or name
              if (prop != "COUNTY_FIP" && prop != "STATE_FIP" && prop != "NAME") {

                //       // push that attribute value into the array
                rates.push(Number(county.properties[prop]));
              }
            }
          });

          // create class breaks
          var breaks = chroma.limits(rates, 'q', 5);

          // create color generator function
          var colorize = chroma.scale(chroma.brewer.OrRd).classes(breaks).mode('lab');




          // verify the result!
          // console.log(rates);
        }
        drawMap(counties, colorize)


      }

      function drawMap(counties) {

        // create Leaflet object with geometry data and add to map
        const dataLayer = L.geoJson(counties, {
          style: function (feature) {
            return {
              color: 'black',
              weight: 1,
              fillOpacity: 1,
              fillColor: '#1f78b4'
            };
          }
        }).addTo(map);

        // first set the zoom/center to the dataLayer's extent
        map.fitBounds(dataLayer.getBounds());

        // then back the zoom level off a bit (since we're viewing the map full screen)
        map.setZoom(map.getZoom() - .2);

        updateMap(dataLayer, colorize, '2006');

      }

      function updateMap(dataLayer, colorize, currentYear) {

        // loop through each county layer to update the color 
        dataLayer.eachLayer(function (layer) {

          const props = layer.feature.properties;

          layer.setStyle({
            fillColor: colorize(Number(props[currentYear]))
          })
        })

      } // end updateMap()```
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...