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)
.addTo(map)
.on('done', function(layer) {
map.layer = layer;
layer.getSubLayer(0).set(subLayerOptions); // sql is inside here
map.invalidateSize(true);
}).on('error', function() {
console.error("Cannot create layer");
});
EDIT
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.
EDIT 2
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.
subLayerOptions.sql
, if this is a local file your code will not work.