I'm trying to use CartoDB API to retrieve a dataset from my account to a heatmap. I want to filter the data using SQL. The problem is the following: the sql query does not always work when using the on done callback. However, it does work when I set it from the console using the map variable (where I stored the layer).

cartodb.createLayer(map, layerUrl)
.on('done', function(layer) {
    map.layer = layer;    
    layer.getSubLayer(0).set(subLayerOptions); // sql is inside here
}).on('error', function() {
    console.error("Cannot create layer");


I wrote another cartodb code in the example, the actual sentence which sets the sql is this one: layer.setSQL(subLayerOptions.sql); (as it is a heatmap and the torque layer seems not to have the getSubLayer function).

To clarify my question, the result I'm obtaining is the same as if I don't set the query in the done callback (as I assume it is using the sql I have in the map I created from CartoDB SaaS). It is ignoring the query passed through javascript. I remark that if I set it later it works fine, it just needs some time to load the first time so I assume the problem is around that but I haven't find it yet.


Sometimes the fix is just to zoom in and out. In this cases the data seems to be loaded but the interface is not refreshing.

  • Please ask your new edited comments in a new question if the first one was solved. Commented Mar 30, 2016 at 16:32
  • It was not answered, I just added information I found that could help solve the issue. Commented Mar 31, 2016 at 8:06
  • If you could share your code or create a repo to have a look. Because with this info I cannot replicate your problem. Be careful with subLayerOptions.sql, if this is a local file your code will not work. Commented Mar 31, 2016 at 10:04

1 Answer 1


With the little info you have told us the only thing I came up is removing map.layer = layer. Also I do not know what are you trying to do with map.invalidateSize(true);. Nevertheless, I have created a working example about setting the options of a sublayer. And the sql query works every time! :-)

  • Sometimes my maps are initialized while hidden and when I show them up the size is wrong, so I invalidate it to fix the problem. Commented Mar 30, 2016 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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