Aggregating, then averaging user inputs from my database.

Hey guys, I realize this has been addressed in other posts however Im still struggling with my code to compute what I’m asking.

This is the code I’m working with below:

import wixData from 'wix-data';

$w.onReady(function () {
    $w("#button1").onClick(() => {
        $w("#pay").setFilter(wixData.filter()
            .eq("position", $w("#dropdown2").value));
    });
});
function populateCalculatedFields() {
 const currentItem = $w("#pay").getCurrentItem();
    $w("#firstpay").text = currentItem.fullName;
    wixData.aggregate("pay")
        .group("bank", "basePay")
        .avg("basePay")
        .run()
        .then((results) => {
 let items = results.items;
 let numItems = results.length;
 let hasNext = results.hasNext();
        })
        .catch((error) => {
 let errorMsg = error.message;
 let code = error.code;
        });

What I’m trying to do is aggregate the salary inputs from a dropdown menu on a dynamic page.

Right now it is saying there is a parsing error, unexpected token. I’ve tried looking into what that is and cant figure it out. Additionally I’m not sure if I’ve set up the code correctly to use the dropdown menu as the filter for the column of data I want (the salary, based on the filter from the particular bank chosen from the dropdown).

Also I’m not sure what the let items, let numItems, let hasNext means, so if that is not necessary I can remove it but it was in the code I saw for something similar.

Thanks,
Gabe

Right now it is saying there is a parsing error, unexpected token…

Where is it in your code? Is it just a simple case of you need to check that you have equal amount of matching pairs of curly brackets and parentheses, which are the open { ( and the closed } ).

Have a read here about aggregating with Wix.
https://support.wix.com/en/article/corvid-working-with-aggregations-in-the-data-api

Fixed, the parsing error, you were right, I went back through and counted them up. I’ll take a look at that article.

Ok so this is the new code I have per the article you referred me to.

import wixData from 'wix-data';

$w.onReady(function () {
    $w("#button1").onClick(() => {
        $w("#pay").setFilter(wixData.filter()
            .eq("position", $w("#dropdown2").value));
    });
});

const filter = wixData.filter($w("#firmName").text).eq("bank");
const having = wixData.filter($w("#dropdown2").value).eq("position");
wixData.aggregate("pay")
    .filter(filter)
    .group("bank")
    .having(having)
    .avg("basePay")
    .run()
    .then((results) => {
        ($w("#firstpay").text)
    });

When I preview, the console says: The element selector function (usually $w) cannot be used before the page is ready .

Regarding the code,
“firmName” is the is the name of the bank that is displayed due to it being a dynamic page and im setting it to match the “bank” (fieldID) within the database “pay”
“dropdown2” is the user input and filters by “position”(fieldID),

At this point it should have filtered by bank, and then by position within the bank. Now I want it to aggregate from the “pay” database by bank and then by position, the data from the “basePay”(fieldID) column

Lastly I want it to display the results in the textbox #firstPay"

Thanks @givemeawhisky

Any thoughts on this?

When I preview, the console says: The element selector function (usually $w) cannot be used before the page is ready.

It might just be a simple case of you needing to make sure that your page and your dataset is loaded first before you can do the actual query,

Move your button onClick event handler down one space and add a dataset onReady function so that your dataset loads with your page.
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#onReady

So something like this…

$w.onReady( () => {
$w("#pay").onReady( () => {
  
 $w("#button1").onClick(() => {
 $w("#pay").setFilter(wixData.filter()
 //rest of code

Obviously, you will have to add the extra parentheses and curly brackets to your code as you have added the extra line too, you might want to add it after the onReady functions or just ad it later to the bottom of your code, your choice.