Error:
Loading the code for the My Profile page.
To debug this code, open vq4en.js in Developer Tools.
An error occurred in one of datasetReady callbacks TypeError:
Cannot read property ‘firstName’ of null
was also getting this error:
Failed to load initial data Error: WDE0028: Request timed out.
Any Ideas? Is it a problem with the database?
Here is the code
Code:
import wixData from ‘wix-data’;
let user;
$w.onReady( function () {
//handler for getting the user information once the dataset is ready
$w(“#updateProfile”).onReady( async function () {
//first grab the user from the dataset
user = $w(“#updateProfile”).getCurrentItem();
//now get the name of the user
$w(“#firstName”).value = user.firstName;
//Check if user exits. If user does not exist, user === null
if (user) {
//user exists, show updateProfileButton and welcome user.
$w(“#updateProfileButton”).show();
$w(“#welcomeUser”).text = Welcome ${user.firstName}
;
$w(“#profileArea”).expand();
//call getReferralCount which returns a number of referalls for current user.
var count = await getReferralCount(user.referralCode)
$w(“#numRef”).text = count.toString();
} else {
//user does not exist, ask them to create their profile.
$w(“#createProfileButton”).show();
$w(“#updateProfileButton”).hide();
$w(“#profileArea”).collapse();
$w(“#welcomeUser”).text = Welcome New User. Please create a profile!
;
}
});
//handler for after the dataset has been updated/saved with new info
$w(“#updateProfile”).onAfterSave(() => {
//get the name of the user and prompt update
$w(“#welcomeUser”).text = “Update Completed!”;
//here we are delaying it by 2500 milliseconds (2.5 seconds) before prompting the welcome
const millisecondsToDelay = 2500;
setTimeout(() => {
$w(“#welcomeUser”).text = Welcome ${user.firstName}
;
}, millisecondsToDelay);
});
//countdown
var countDownDate = new Date().getTime();
//$w(“#countDownTimer”).text = hours + "72 " + minutes + "m " + seconds + "s ";
});
/**
- getReferralCount(referralCode) returns the total number of times the user’s referralCode appears in the
- profile database.
*/
export async function getReferralCount(referralCode) {
//first lets find it.
let query = await wixData.query(“profile”).eq(“referredBy”, referralCode).find();
//return the count
return (query.items.length);
}