Search code examples
databasesqlitecollectionsappcelerator

How to output result of a SUM SQL query in Appcelerator?


The query below calculates the sum of the fields ("real") in the collection and correctly shows the results in the console. But how can I populate a text label with the result?

var fetchedTranslations = $.translationData;
fetchedTranslations.fetch({
    query: 'SELECT SUM (sum_words) FROM "translationsCollection"'
});
console.log("result: " + JSON.stringify(fetchedTranslations)); //result: [{"SUM (sum_words)":42}]
$.wordCounter.text = ; //should show 42

SOLUTION

When renaming as Larrie suggested and using the .toJSON() method it works

var result = fetchedTranslations.toJSON();
$.wordCounter.text =  result[0].sum_total;    

Solution

  • Change your query to:

    SELECT SUM (sum_words) AS sum_total FROM "translationsCollection"
    

    Then you can access

    $.wordCounter.text = fetchedTranslations[0].sum_total;