Help With Queries On User Login

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;
}
}
)

Hi,
You don’t need to use renderCycle for what you are trying to achieve, check out this code that checks whether a user is logged in, get his data from the database and saves it to the session storage.

import wixUsers from 'wix-users';
import wixData from 'wix-data';
import {session} from 'wix-storage';

$w.onReady(function () {
if (wixUsers.currentUser.LoggedIn){
    let userId = wixUsers.currentUser.id;
    wixData.query("Business")
    .eq("adminId", "userId")
    .find()
    .then( (results) => {
     let info = results.items[0]; 
     session.setItem("hasProfile", "True");
     session.setItem("profileUrl", info.profileUrl);
     session.setItem("dashboardUrl", info.dashboardUrl);
     session.setItem("firstName", info.firstName); 
    } )
   .catch( (err) => {
   let errorMsg = err;
    } );   
}
}

Good luck :slight_smile:
Or

Hi Or, I made the changes to my code to remove the rendering part as you suggested, but I’m still running into an issue I hope you can help with. The issue comes when I attempt to use the URLs I retrieved to direct a user to a page when a button is clicked. For example, I have a button labeled Dashboard. Which when clicked, I want to direct the user to the dashboard URL I just pulled out of the data collection. As I need this dashboard button to appear on all pages I’m hoping to declare its onClick event in the site portion of my code. The code in the onClick event looks like this:

Let Url = session.getItem(‘dashboardUrl’);
wixLocation.to(Url);

The error I receive in the Firefox developer console states “to cannot be set to type Value, it must be set to type String”. From what I understand though from reading the documentation for session.getItem, the returned values are always strings. So I can’t figure where this error is coming from. Can you assist?