Calculate the items in Database and Show the Result as a Text

I have created a daily crop quantity database for 2 different areas (A & B) as shown below.
I need to get the sum of each daily crop quantity as Total Crop in the month end, Area wise (For A & B).


So I used below code on “Total crop page” to change #text9 according to the database.
But its not working.

import wixData from ‘wix-data’;
$w.onReady( function () {

wixData.query(“CropEntry”)
let filter = wixData.filter().eq(“area”, “A”);

wixData.aggregate(“CropEntry”)
.filter(filter)
.sum(“cropQty”, “sumcropQtyA”)
.run()
.then( (results) => {
if (results.items.length > 0) {
let items = results.items; // see below
let numItems = results.length; // 1
let hasNext = results.hasNext(); // false

console.log(results.items);
$w(“#text9”).text = items.toString();

} else {
// handle case where no matching items found
}
} )
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
} );
})

Appreciate your great help on this <3.
@ziv-shalev

Hi,
I see that the items variable can contain multiple elements as it’s an object of items from the collection. Click here to learn more.
You need to limit the query to the first found item:

let firstItem = results.items[0];

Then you need to get the relevant value from the results:

let item = firstItem.fieldkey; // e.g.  for example if the *  firstItem is:
 *
 *  {
 *    "_id":          "00001",
 *    "_owner":       "ffdkj9c2-df8g-f9ke-lk98-4kjhfr89keedb",
 *    "_createdDate": "2017-05-24T12:33:18.938Z",
 *    "_updatedDate": "2017-05-24T12:33:18.938Z",
 *    "title":        "Mr.",
 *    "first_name":   "John",
 *    "last_name":    "Doe"
 *  }
 */ then the variable you need may be defined as:
 let item = firstItem.title; 

If it’s not string, you can stringify it as in your code.