Help needed with vehicles comparison calculator page not working

Help needed with vehicles comparison calculator page not working

Hello everyone,

I’ve hit a bit of a snag with a project I’m working on and could really use your insights. I’m building a site on Wix with Velo that includes a main calculator page where users input data (fuel price, annual distance, and fuel efficiency) and a comparison calculator page to compare these inputs against a second set of data. The idea is to calculate and display potential savings or extra costs based on these comparisons.

Here’s what I’ve done so far:

  1. Database Setup:I created a database collection named CalculatorData with fields for fuelPrice, annualDistance, and fuelEfficiency. I made sure the field names and types are correctly set up and added validation rules for data integrity.

  2. Backend Code: I wrote backend functions using Velo to save data from the main calculator page and retrieve it for the comparison calculator page. I’ve double-checked these functions for errors and made sure they align with the database field names and types.

  3. Frontend Code: On the frontend, I’ve got code to call these backend functions. The main calculator page successfully saves the data (as far as I can tell), but when I navigate to the comparison calculator page, it doesn’t display the data as expected.

  4. Debugging Attempts: I’ve gone through error messages in the console, checked my database permissions, ensured my frontend code correctly matches the IDs of my site elements, and even tried hardcoding data into the frontend to bypass the database step. Despite all this, the comparison page still isn’t working correctly.

The problem: The comparison calculator page either fails to retrieve the data for comparison, or something goes wrong in the process, and no results are displayed. I’ve checked for console errors, ensured my element IDs are correct, and even tried simplifying my logic to isolate the problem, but no luck.

What I’m looking for: If anyone has encountered similar issues or can spot a mistake in my approach, your guidance would be invaluable. Tips on better debugging strategies or any resources that could help would also be greatly appreciated.

Thanks a ton for taking the time to read through my issue. Any help or suggestions you could offer would be a huge relief and incredibly appreciated.

Looking forward to your insights,

The best way to get help on this here is to share the section of code that is not working. It sounds like something in the frontend code is broken between the saving and retrieval of this data.

Hello and thank you for your guidance on addressing my issue with the comparison calculator on my Wix site. I appreciate your suggestion to share the problematic section of the code for better assistance. I’d like to clarify my current implementation approach and the specific challenge I’m facing.

Implementation Approach:
I am not directly using backend and frontend code (in the traditional sense of Wix Corvid APIs or JavaScript) to save and retrieve the data for my comparison calculator. Instead, I’m utilizing Wix’s built-in datasets feature to manage the data interaction between the user input on the main calculator page, the database, and the comparison calculator page.

Main Calculator Page:
On the main calculator page, users input data related to Vehicle 1 (such as annual distance, fuel price, and fuel efficiency), and the page calculates the annual fuel cost. This result, along with the inputs, is saved to a Wix database collection via a dataset connected to the input form.

Comparison Calculator Page:
The comparison calculator is intended to fetch the saved result (annual fuel cost of Vehicle 1) from the database, allow users to input fuel efficiency for Vehicle 2, and compare the two vehicles to determine which is more economical.

Issue Encountered:
Despite setting up the dataset to retrieve the necessary data for the comparison, the comparison calculator page consistently displays the same result, stating that “both vehicles are equally economical based on annual fuel cost,” irrespective of the different inputs provided for Vehicle 2’s fuel efficiency.

This leads me to believe there might be an issue with how the dataset retrieves data for the comparison or possibly how the comparison logic is executed based on the retrieved data. However, the specific problem does not seem to lie within custom frontend/backend code but rather in the interaction with the dataset and possibly the logic determining the displayed result.

Request for Assistance:
Given this setup using Wix datasets, could there be any known issues or common pitfalls that might lead to this behavior—where the dataset either does not correctly retrieve the data needed for the comparison, or the logic tied to the dataset output behaves unexpectedly? Any insights, especially related to dataset configuration or potential oversights in setting up dynamic interactions using datasets, would be greatly appreciated.

Thank you once again for your time and assistance. I’m looking forward to any suggestions that could help resolve this issue with my comparison calculator.

javascript
$w.onReady(function () {
$w(“#compareButton”).onClick(() => {
const fuelEfficiencyVehicle2 = parseFloat($w(“#efficiencyVehicle2”).value);
if (isNaN(fuelEfficiencyVehicle2) || fuelEfficiencyVehicle2 <= 0) {
$w(“#comparisonResultText”).text = “Please enter a valid fuel efficiency for Vehicle 2.”;
return;
}

    const latestData = $w("#dataset1").getCurrentItem();
    if (!latestData) {
        $w("#comparisonResultText").text = "Error: No data found. Please ensure you've entered data on the main calculator page.";
        return;
    }

    const fuelEfficiencyVehicle1 = latestData.fuelEfficiency;
    const annualDistance = latestData.annualDistance;
    const fuelPrice = latestData.fuelPrice;
    const annualFuelCostVehicle1 = (annualDistance / fuelEfficiencyVehicle1) * fuelPrice;
    const annualFuelCostVehicle2 = (annualDistance / fuelEfficiencyVehicle2) * fuelPrice;
    const costDifference = annualFuelCostVehicle1 - annualFuelCostVehicle2;

    let comparisonResult;
    if (costDifference > 0) {
        comparisonResult = `Vehicle 1 is more economical, saving $${Math.abs(costDifference).toFixed(2)} annually compared to Vehicle 2.`;
    } else if (costDifference < 0) {
        comparisonResult = `Vehicle 2 is more economical, saving $${Math.abs(costDifference).toFixed(2)} annually compared to Vehicle 1.`;
    } else {
        comparisonResult = "Both vehicles are equally economical based on annual fuel costs.";
    }

    $w("#comparisonResultText").text = comparisonResult;
});

$w("#resetButton").onClick(() => {
    $w("#efficiencyVehicle2").value = ""; 
    $w("#comparisonResultText").text = ""; 
});

});

This is the overview/Explanation of the code;

Certainly! The code snippet provided for the comparison calculator on a Wix site is designed to compare the annual fuel costs of two vehicles based on user inputs and database-stored values. Here’s a breakdown of how it works:

Code Overview

javascript
$w.onReady(function () {

This line ensures that the code inside it will run only after the Wix page elements have finished loading, ensuring that all elements can be interacted with via code.

    $w("#compareButton").onClick(() => {

This sets up an event listener for a click event on an element with the ID compareButton. When this button is clicked, the code inside this function will execute. This button is meant to trigger the comparison calculation.

        const fuelEfficiencyVehicle2 = parseFloat($w("#efficiencyVehicle2").value);

This retrieves the user’s input for Vehicle 2’s fuel efficiency from an input element with the ID efficiencyVehicle2, converts it to a floating-point number (to ensure it’s treated as a numerical value for calculations), and stores it in the variable fuelEfficiencyVehicle2.

        if (isNaN(fuelEfficiencyVehicle2) || fuelEfficiencyVehicle2 <= 0) {
            $w("#comparisonResultText").text = "Please enter a valid fuel efficiency for Vehicle 2.";
            return;
        }

This checks if the input for Vehicle 2’s fuel efficiency is not a number (isNaN) or is less than or equal to zero. If either condition is true, it displays an error message in an element with the ID comparisonResultText and stops further execution of the function to prevent incorrect calculations.

        const latestData = $w("#dataset1").getCurrentItem();

This retrieves the current item (the latest or selected entry) from a dataset connected to a collection (which stores Vehicle 1’s data) with the ID dataset1. The retrieved data is stored in the variable latestData.

        if (!latestData) {
            $w("#comparisonResultText").text = "Error: No data found. Please ensure you've entered data on the main calculator page.";
            return;
        }

This checks if any data was successfully retrieved from the dataset. If not, it displays an error message in the comparisonResultText element and stops further execution.

        const fuelEfficiencyVehicle1 = latestData.fuelEfficiency;
        const annualDistance = latestData.annualDistance;
        const fuelPrice = latestData.fuelPrice;
        const annualFuelCostVehicle1 = (annualDistance / fuelEfficiencyVehicle1) * fuelPrice;
        const annualFuelCostVehicle2 = (annualDistance / fuelEfficiencyVehicle2) * fuelPrice;
        const costDifference = annualFuelCostVehicle1 - annualFuelCostVehicle2;

These lines calculate the annual fuel costs for both Vehicle 1 and Vehicle 2 using the formula (Annual Distance / Fuel Efficiency) * Fuel Price, where fuelEfficiencyVehicle1 comes from the dataset, and fuelEfficiencyVehicle2 is user input. The difference in annual fuel costs between Vehicle 1 and Vehicle 2 is then calculated and stored in costDifference.

        let comparisonResult;
        if (costDifference > 0) {
            comparisonResult = `Vehicle 1 is more economical...`;
        } else if (costDifference < 0) {
            comparisonResult = `Vehicle 2 is more economical...`;
        } else {
            comparisonResult = "Both vehicles are equally economical...";
        }

Based on the value of costDifference, this section determines which vehicle is more economical or if they are equally economical. It prepares a message accordingly.

        $w("#comparisonResultText").text = comparisonResult;

This displays the prepared comparison result message in the element with the ID comparisonResultText.

    });

    $w("#resetButton").onClick(() => {
        $w("#efficiencyVehicle2").value = ""; 
        $w("#comparisonResultText").text = ""; 
    });
});

This sets up an event listener for the resetButton. When clicked, it clears the input for Vehicle 2’s fuel efficiency and the comparison result text, allowing users to perform a new comparison.

In summary, this code is responsible for fetching input data, performing a comparison calculation between two vehicles based on fuel efficiency and other parameters, and displaying the result of this comparison to the user.