PDA

View Full Version : Store.query, .sum gives NaN, .each give 1 row too many



JavascriptParrot
9 Nov 2012, 7:49 AM
Hello all,


I have a store with some records and fields that are summed, I use the Sum function to get the total of the field Quantity. Next I need to know the total of the records where a property is set to true. There is one row at this time that meet this filter. I use the query function to retrieve the row(s), but when using the sum function it gives me NaN, using the each function it gives me two records? But the last record is undefined, here some code to explain.


var quantityTotal = ArtStore.sum("Quantity"); //is 500
var FilteredRecords = ArtStore.query("field", true);
//FilteredRecords.length = 1
//FilteredRecords.sum("Quantity") = NaN
FilteredRecords.each( function(rec){
console.log(rec);
});
//i {phantom: true, internalId: "ext-record-39", raw: Object, data: Object, modified: Object…}
//undefined


There is an undefined record in the collection, where does it come from? It screws up the sum function I think.

Thanks!

mitchellsimoens
14 Nov 2012, 10:45 AM
Since you are doing two "sums" I would just iterate through the store with store.each and have a couple variables to add to based on the values of the fields.