Hi, I’m running into an issue with my website functionality, and I was hoping to get some assistance with it. I’m attempting to have a data collection query run once a user has logged in to my site, and to store some information from the returned results in session storage for use on other pages. So far though I’m having no luck, and I can’t figure out why. The process I’m following goes like this:
The user clicks a login button in my site header, which fires a login function in my site code. Once the user successfully logs in, they are redirected to my homepage via a wixLocation.to call in an OnLogin event in my site code. Once the now logged in user is returned to my homepage, a query is ran against one of my data collections, pulling that user’s specific record by use of the wix unique userId, which was captured and stored in a field of my collection previously. In the OnReady function of my page, the query is supposed to finish executing and return the user’s record. At which time certain information (URLs to the user’s dashboard and profile page, and the first name of the user) are to be placed in session storage for use on other pages. Where I’m running into issues is that I can tell that the query is returning results, but I am unable to access the portions of the record that I need. When I attempt to they keep returning blank or null.
The code for my homepage is below. I was hoping that someone here could take a look through it, and give me some guidance as to where I may be going wrong? Any help would be greatly appreciated, as I’ve been stuck on this for the last several days.
//Import Statements
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
import wixWindow from ‘wix-window’;
import {session} from ‘wix-storage’;
//Global page code
//Variable definitions
let queryPromise;
let record;
//Running the query
if (wixWindow.rendering.renderCycle === 1){
if (wixUsers.currentUser.LoggedIn){
let userId = wixUsers.currentUser.id;
queryPromise = wixData.query(“Business”)
.eq(“adminId”, userId)
.find();
}
}
$w.onReady(function () {
//Once the query finishes executing return results
if (wixWindow.rendering.renderCycle === 1){
queryPromise.then((results) =>{
if (wixWindow.rendering.env === “backend”){
return results.items;
}
record = results.items;
});
}
else {
record = wixWindow.rendering.warmupData;
}
//Populate Session Storage with returned values
if (wixWindow.rendering.renderCycle === 1){
if (wixWindow.rendering.env === “browser”){
//Check for a null record for testing purposes
if (record === null){
$w(‘#Welcome’).text = “Testing for length”;
$w(‘#Register’).show();
}
else {
//Populate Session Storage and page elements
let info = record[0];
session.setItem(“hasProfile”, “True”);
session.setItem(“profileUrl”, info.profileUrl);
session.setItem(“dashboardUrl”, info.dashboardUrl);
session.setItem(“firstName”, info.firstName);
$w(‘#Welcome’).text = “Welcome " + info.firstName + " !”;
}
}
}
//Delay all other page code until the query finishes executing
if (wixWindow.rendering.renderCycle === 1){
return queryPromise;
}
}
)