For my hiking website, i would like to have them submit and log their own hikes. I have this running and it feeds into a data set that displays on a repeater but as part of this i would like to also display their lifetime stats such as total kilometers hiked. Is there any way to tally up data submitted by solely that user to give me a total number. I have set up a number field and they can submit to them just don’t know how to tally it all up.
If you could help me that would be great. (please provide code example or explanation)
Yes, you can use the wixData.aggregate() method to aggregate all kilometers hiked by each hiker.
Like this:
import wixData from "wix-data"
$w.onReady(async () => {
//You can dynamically assign the user here
const user = "a31c8da9-0c74-4329-a2ae-0e323671eefb"
const retriveKm = await aggregateKm(user)
const userKm = retriveKm[0].kilometersSum
console.log(userKm) //just checking if the sum is ok
})
async function aggregateKm(user) {
//Creates a filter for the specified user ID
const filter = wixData.filter().eq("user", user)
//Aggregates the data based on the specified user and dataset column
const data = await wixData
.aggregate("Hikes") //Dataset of the Hikes
.filter(filter)
.sum("kilometers")
.run()
return data.items
}
how would i use “owner” id to sort this as my data base is as simple as a number input that is sorted using a filter for owner = Current user. I am very new to coding so im still a little confused.
Is there any way for the final number to be displayed in a final text box?
Can you post your code?
And can you show me your dataset/database?
As I imagine, you should have a dataset for all the hikes uploaded to your website, that I called “Hikes” . Inside this dataset you should have a field “user”/“owner” that could be a REFERENCE to other dataset, that I called “Hikers” , or just a username like “Bruno Prado” .
And this is the lifetime stats area that i would like to display combined data we discussed about.
My code from earlier has been deleted but i will paste what code I copied from you.
import wixData from "wix-data"
$w.onReady(async () => {
//You can dynamically assign the user here
const user = "a31c8da9-0c74-4329-a2ae-0e323671eefb"
const retriveKm = await aggregateKm(user)
const userKm = retriveKm[0].kilometersSum
console.log(userKm) //just checking if the sum is ok
})
async function aggregateKm(user) {
//Creates a filter for the specified user ID
const filter = wixData.filter().eq("user", user)
//Aggregates the data based on the specified user and dataset column
const data = await wixData
.aggregate("salesLead02") //Dataset of the Hikes
.filter(filter)
.sum("numberField")
.run()
return data.items
}
Ok, first things first, do you have a different area that you see the list of hikes done by a specific owner ? Because if you do, we can use a different code.
Send me the link so I can navigate and better understand.
import wixData from "wix-data"
import wixUser from "wix-user"
$w.onReady(async () => {
//You can dynamically assign the user here
const currentUser = wixUser.currentUser
const getTotalDistance = await aggregateDistance(currentUser._id)
console.log(getTotalDistance)
const currentUserDistance = getTotalDistance[0].distanceSum
$w("#text69").text = currentUserDistance.toString()
//Change $w('#dataset1') to the dataset element connected to the Log Hike Collection
$w("#dataset1").onReady(async () => {
const numberOfHikes = $w("#dataset1").getTotalCount()
//Change $w('textNumberOfHikes') to the text element that displays "Total hikes done:"
$w("#textNumberOfHikes").text = numberOfHikes.toString()
})
})
async function aggregateDistance(currentUser) {
const filter = wixData.filter().eq("owner", currentUser)
const data = await wixData
.aggregate("logHike") //Change this to the Dataset name
.filter(filter)
.sum("distance") //Change this to the field key name of the DISTANCE (KM) column
.run()
return data.items
}
Remember to change the things that I commented in the code.
It still doesn’t seem to be working, Would i need to connect the actual text boxes to the data set itself and if so what column would i connect them to?
Once again, thanks so much for taking the time to help me.