1

How can I retrieve the result of a SQL query from a source?

I would like to create a dynamic table that displays the result of a query.

 const source= new carto.source.SQL('SELECT cartodb_id, country, pop, the_geom_webmercator, the_geom FROM country');

const layer= new carto.layer.Layer(source, style, {
    featureOverColumns: ['cartodb_id', 'country', 'pop', 'the_geom']
});

layer.on('featureOver', data => {
    console.log(data);
});

 source.setQuery('SELECT * FROM country WHERE pop > 10000')
        .catch(cartoError => {
        console.log(cartoError)
        })
        .then(success => {
            //Display result
            console.log(success); //undefined
        });

I can't get the result back. setQuery returns a Promise object, but it undefined.

I guess I miss something... any idea ?

1

CARTO.js does not retrieve raw data from the account, on that code you are just setting the map data definition.

To get raw data you need to use the SQL API. There is no dedicated SDK for that API because of its extreme simplicity. You have code examples on using fetch and other libraries to do that on this help center article.

Disclaimer: I work at CARTO.

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.