Hi,
I have this web site that has a dynamic page to acquire additional information about new members.
This is all working fine.
I coded a mean of finding out if the logged user is new to the site by checking the 8 required fields if something has been entered.
I would like to as soon as they get to that dymanic page with the 8 input fields and check if they are empty then stay on the dynamic page so they enter the required infos. If they field are already been filledbcause they are not a new memeber, then redirect them to my home page using wixLocation.to.
This all works fine except that as soon as a create a button to direct the existing member to change something on his profile, I have him to go to my dynamic page but since I have a wixLocation.to in the Ready section, it goes back to my home page.
Any ideas how I can skip my dymanic page (form) is all the required info is there and have a button that will redirect the member to this dynamic page for update without automatically go to the home page.
Pierre Lacasse
Onreadyrun a function on the back end that checks if the user meets the new user criteria, then with the results run an if that directs user to the update profile. Keep in mind that content blockers prevent the wixlocation.to to run onready.
Hi Carlos,
thank you for the answer.
I have been trying to implement this but I need some help.
I get this error :
console.js:35 Error loading web module backend/dataService.jsw: Cannot find module ‘wix-location’
Should this code work?
Thank you for helping!
Pierre Lacasse
Here is my backend called dataService.jsw
//dataService.jsw
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
import wixUsers from ‘wix-users’
export function skipProfile() {
return wixData.query(“ListeMembres”)
.eq(“adresseCourriel","placasse47@gmail.com”)
//.limit(10)
.find()
.then( (results) => {
//let items = results.items;
let profil = results.items[“profil”];
console.log (profil)
let autorisation = results.items[“autorisation”];
console.log (autorisation)
if (autorisation ===“À valider”) {
wixLocation.to(/ListeMembres/Update/${wixUsers.currentUser.id}
);
}
else {
if (profil === “Oui”) {
wixLocation.to(“/Accueil”);
}
else {}
}
})}
I am calling skipProflie function from my profile/update page:
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
//import wixData from 'wix-data
import {skipProfile} from ‘backend/dataService’;
$w.onReady( function () {
skipProfile()
$w(“#input9”).hide();
$w(“#text26”).hide();
$w(“#input10”).hide();
$w(“#button24”).show();
$w(“#button40”).show();
$w(“#text35”).hide()
.
.
.
.
wix-users is not available in the backend. As stated in the API:
The wix-users module contains functionality for working with your site’s users from client-side code.
You need to use wix-users-backend as stated in the API:
The wix-users-backend module contains functionality for working with your site’s users from backend code.
Thank you Yisrael.
Can you help me with the described project from my initial posting?
In summary,
after my LogIn page, I want to be able to skip my dynamic page if some criterias are met.
I have the required data in my Collection ie if user is activated or not and if all the required fields are all filled in or not.
From there I either skip the dynamic page or go to another page.
If I use the if logic and wixLocation.to on my Ready section of my dynamic page and then it works but if I define a button elsewhere on my site to go back to my profile page which is my dynamic page, it checks the criterias and skip the dymanic page still. Any suggestions on how to do this?
I was suggested this by Carlos (see my questioning in bold)
On ready (on my dynamic page or another page?) run a function on the back end that checks if the user meets the new user criteria, then with the results run an if (in the backend function or dymanic page?) that directs user to the update profile. Keep in mind that content blockers prevent the wixlocation.to to run on ready.
Thank you
It works this way.
Onready: WixUsers.getcurrentuser then pass the user ID to a back end function that will return the data you have for the user. After you get the data back you will determine wether the user meets your criteria and redirect the user accordingly using WixLocation.to.
I’ve noticed that wixLocation.to does not work if there Is a content blocked enabled and the GET process might take about 1 sec. So it’ll be better to figure out a process. I’ll recommend not to run the code onReady but do it with an event, for example if you have a custom sign in form then run it before the user logs in so it is redirected right away. And if you have a button that is called my account or something then use and if statement to add the proper link to it.
import wixUsers from 'wix-users';
import {getUser} from 'backend/aModule';
import wixLocation from 'wix-location';
$w.onReady(function () {});
export function loginButton_click(event, $w) {
let email = $w("#email").value;
let password = $w("#password").value;
wixUsers.login(email, password)
.then( () => {
redirect(); //See this function below
} )
.catch( (err) => {
$w('#text38').expand();
console.log(err);
} );
}
export function forgotPassword_click(event, $w) {
wixUsers.promptForgotPassword();}
function redirect () {
let id = wixUsers.currentUser.id
getUser(id).then(product => { //This is waht you get from the backend
if (product.name === '') {var name = false}
if (product.phone === '') {var phone = false}
if (product.adress === '') {var adress = false}
if (name === false && phone === false && adress === false)
{wixLocation.to('https://example.com/users/profile'+id)} //Your page if the profile is incomplete
else {wixLocation.to('https://example.com/users'+id)} //your page if the profile is complete
})
.catch(error => {
console.log(error);
});}
Then on the back end you’ll have
export function getUser (id) {
let options = { "suppressAuth": true, "suppressHooks": true };
return wixData.get("myCollection",id, options)}