2

Can anyone tell me why I am getting this error? It happens when I try and filter by a layer action. I also tried to do the cartodb tutorial on "Toggle the Map View" following it line for line and am getting the same error.

var map;
function init(){
  // initiate leaflet map
  map = new L.Map('map', {
    center: [39,-95],
    zoom: 4,
    minZoom: 3      
  })

  L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
      attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
  }).addTo(map);

  var layerUrl = 'https://myusername.cartodb.com/api/v2/viz/88afc111-16cf-11e5-a0d9-0e9d021ea90d/viz.json';

  var layerOptions = {
            query: "SELECT * FROM mytable_1",
            legends:false,
            cartodb_logo: false
  }

  cartodb.createLayer(map, layerUrl, layerOptions)
   .on('done', function(layer) {

    map.addLayer(layer);
  }).on('error', function() {
    //log the error
  });

  var LayerActions = {
    all: function(){
        layer.getSubLayer(1).setQuery("SELECT * FROM mytable_1");
        return true;
      },
    one: function(){
        layer.getSubLayer(1).setQuery("SELECT * FROM mytable_1 WHERE overall_rating ILIKE '%1%'");
        return true;
      },
    two: function(){
        layer.getSubLayer(0).setQuery("SELECT * FROM mytable_1 WHERE overall_rating ILIKE '%2%'");
        return true;
      },

    three: function(){
        layer.getSubLayer(0).setQuery("SELECT * FROM mytable_1 WHERE overall_rating ILIKE '%3%'");
        return true;
      },
    four: function(){
        layer.getSubLayer(0).setQuery("SELECT * FROM mytable_1 WHERE overall_rating ILIKE '%4%'");
        return true;
      },
    five: function(){
        layer.getSubLayer(0).setQuery("SELECT * FROM mytable_1 WHERE overall_rating ILIKE '%5%'");
        return true;
      },
    na: function(){
        layer.getSubLayer(0).setQuery("SELECT * FROM mytable_1 WHERE overall_rating ILIKE '%NA%'");
        return true;
      }
  }
  $('li').click(function(){
    $('li').removeClass('selected'); $(this).addClass('selected');
    LayerActions[$(this).attr('id')]();

  })
}
1

We would need to know more about your map to help you further, but please make sure that:

  • You're accessing to the layer you want. You're using both layer.getSubLayer(1) and layer.getSubLayer(0), not sure if you're aware of this :-)
  • Make sure "layer" is defined. Javascript might be accessing a variable which hasn't been declared yet. You could move this piece of code inside the onDone callback of createLayer to make sure that the layer exists when you try to access to it
  • Which CartoDB.js version are you using? Please notice that the non-versioned URL (http://libs.cartocdn.com/cartodb.js/v3/cartodb.js) is no longer supported and imports an old version (3.11.23, the last one of 3.11 is 3.11.38) and you might use a versioned one now -- like http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js. I mention this because the latest changes in CartoDB.js were due to layer management.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.