I am trying to get a sum of a column (hours) from one of my databases filtered by member to populated data on a members only page.
I keep getting errors, the lastest one is Failed to load initial data. Here is my code
// For full API documentation, including code examples, visit https://wix.to/94BuAAs
import wixData from 'wix-data';
import wixUsers from 'wix-users';
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;
user.getEmail()
.then( (email) => {
let userEmail = email;
} );
$w.onReady(() => {
Sum_amount();
});
let having = wixData.filter().eq("_logid",userId)
export function Sum_amount(){
wixData.aggregate("items")
.group("_logid")
.having(having)
.sum("hours","sumHours")
.run()
.then( (results) => {
$w('#input1').value = results.items[0].sumHours; //display total in input field at bottom of table
});
}
My data base is Dive Training Logs (Items)
You might want to try adding an onReady function to your dataset as well so that it is loaded up just after your page.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#onReady
Examples
Register a callback to run after the dataset is ready
$w("#myDataset").onReady( () => {
console.log("The dataset is ready");
} );
onReady( )
Adds an event handler that runs when the dataset is ready.
Description
The onReady() function allows you to optionally perform actions right after the dataset has loaded its data from the collection and updated all connected page elements with their corresponding values.
You can try something like this, however I know that this might give you an error somewhere.
import wixData from 'wix-data';
import wixUsers from 'wix-users';
$w.onReady(() => {
$w("#yourdataset").onReady( () => {
Sum_amount();
});
});
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;
user.getEmail()
.then( (email) => {
let userEmail = email;
});
let having = wixData.filter().eq("_logid",userId)
export function Sum_amount(){
wixData.aggregate("items")
.group("_logid")
.having(having)
.sum("hours","sumHours")
.run()
.then( (results) => {
$w('#input1').value = results.items[0].sumHours;
});
}