top of page
Name should appear here
If loaders don't disappear after 20 seconds reload the page.
It will mean that you have encountered the TypeError.

I experience an occasional problem with a TypeError when opening a page such as this where the fields are filled from a database.  The error is unpredictable and you may have top reload many times to reproduce it.

There is a screenshot of the error and console after the code below.

velo code:

import wixData from 'wix-data';
import wixWindowFrontend from 'wix-window-frontend';

 

const MAIN_DATABASE = "test02";
const LOGIN_EMAIL = "another@another.com";
const NUMBER_OF_LOADERS = 2;

let userId = "";
let showLoadersState = false
let currentUser = null


$w.onReady(function () {
    if (wixWindowFrontend.rendering.env === "browser") {
        initializeUserData();
    } else {
        console.log("Not running in browser environment");
    }
});

async function initializeUserData() {
    try {
        showLoadersState = true;
        showLoaders();
        currentUser = await getUserData(LOGIN_EMAIL);
        if (currentUser) {
            userId = currentUser._id;
            displayUserInfo(currentUser);
            showLoadersState = false;
            hideLoaders();
        }
    } catch (error) {
        console.error("Error initializing user data:", error);
        showLoadersState = false;
        hideLoaders();
        collapseLoaderMessage();
    }
}

async function getUserData(loginEmail) {
    const results = await wixData.query(MAIN_DATABASE)
        .eq("loginEmail", loginEmail)
        .find();

    if (results.items.length === 1) {
        return results.items[0];
    } else if (results.items.length > 1) {
        throw new Error("Multiple users found with the same email");
    } else {
        throw new Error("User not found");
    }
}

function displayUserInfo(user) {
    $w('#firstName').value = user.firstName || '';
    $w('#lastName').value = user.lastName || '';
    $w('#artistNameForDisplay').text = `${user.firstName} ${user.lastName}` || '';
    collapseLoaderMessage();
}

function showLoaders() {
    setLoadersState('show');
}

function hideLoaders() {
    setLoadersState('hide');
}

function setLoadersState(state) {
    for (let i = 0; i < NUMBER_OF_LOADERS; i++) {
        $w(`#loader${i}`)[state]();
    }
}

function collapseLoaderMessage() {
    $w(`#loaderMessage`).collapse();
}

bottom of page