Hi folks,
I have been playing with the aggregation and have what I’m looking to achieve but I feel that there must be a cleaner way to achieve what I’m doing. Basically I want to count the participants for a specified item that are on the same date.
I.e. Item 1, 25th Jan, 4 people
I have this working fine and get the expected results. My question is, how is it best to get other data from the database to work with this in the array? The code is below
async function getUniqueItems() {
const filter = wixData.filter().between('date', new Date, endDate).ne('minimumParticipants', "Private");
let data = await wixData.aggregate("CourseAvailability").group("title", "date")
.ascending("date").filter(filter).max('maxParticipants').min('courseCategory').sum('numberOfParticipants').run();
let items = data.items;
items.forEach((el, i) => items[i]._id = gettRandomID());
console.log(items)
$w('#repeater1').data = items;
$w("#repeater1").onItemReady(($item, itemData, index) => {
if(itemData.numberOfParticipantsSum >= itemData.maxParticipantsMax) $item('#container1').collapse();
$item("#courseImage").src = itemData.image;
$item('#courseImage').tooltip = itemData.title;
$item("#courseTitle").text = itemData.title.toUpperCase();
$item('#date').text = itemData.date.toDateString();
$item('#courseInfoButton').onClick((event) => {
wixLocation.to("/courses/" + encodeURI(itemData.courseCategoryMin) + "/" + encodeURI(itemData.title))
$item('#courseInfoButton').label = "Loading...";
});
});
}
As you can see I have used the Min to access some other info that I want to use from the database. I would also like to use the ‘image’ field from the database. What is the best method to achieve this? Do I need to run a sperate query? It seems like I should be able to access all of the database field keys but that’s the area I’m struggling with.
Also, I have an issue where the title has spaces so I had to use a randomId to let me build the array
This photo may help with clarity
Thanks as always!