Hi just a short intro, I have just recently started out trying wix (a month ago) and I have some basic Javascript programming experience a few years back. Soo I am kind of comfortable with using Wix api and its usage of javascript.
Currently, I am developing a website and working on trying to do basic pre-populating of form inputs based on user’s information(i.e once user has logged in to wix). This works by retrieving their email address and querying our database collection to find a match of their email. Once it matches, it is suppose to retrieve their relevant information and display it into the form inputs on the update page.
I am encountering an issue where my custom code is working on the preview mode but not on the published site and I can’t seem to figure out why. I have contacted wix service centre but they are unable to help because they do not deal with custom code and instead directed me to this forum to make a request for help. Would really really appreciate any developers that are experienced with wix to help me out on this!
Here is the custom code that works on the preview site and the relevant screenshot:
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
$w.onReady(function () {
let userEmail = ‘davidlim@hotmail.com’; //dummy useremail
//This should be the actual published code
/*let user = wixUsers.currentUser;
wixUsers.onLogin( (user) => {
user.getEmail()
.then( (email) => {
userEmail = email; // “davidlim@hotmail.com”
}); */
wixData.query(‘Members’) //Query ‘Members’ database
.contains(‘academic_email’,userEmail) //Find the logged user email inside the member database
.find()
.then(res => {
console.log(userEmail);
let result = res.items[0]['academic_email']; //retreive the email that was matched
if(result === userEmail){ //Filled the input forms with database values from the database
$w('#input1').value=res.items[0]['full_name'];
$w('#input2').value=res.items[0]['academic_email'];
$w('#input4').value=res.items[0]['country'];
$w('#input3').value=res.items[0]['team'];
}else{
$w('#input2').value='not working';
}
});
});
The code works on the preview site as shown above
Here is the custom code that I have make a slight adjustment which it is suppose to work on the publish site
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
$w.onReady(function () {
let user = wixUsers.currentUser;
//This should be the actual published code
wixUsers.onLogin( (user) => {
user.getEmail()
.then((email) => {
userEmail = email; // “davidlim@hotmail.com”
});
wixData.query(‘Members’) //Query ‘Members’ database
.contains(‘academic_email’,userEmail) //Find the logged user email inside the member database
.find()
.then(res => {
console.log(userEmail);
let result = res.items[0]['academic_email']; //retreive the email that was matched
if(result === userEmail){ //Filled the input forms with database values from the database
$w('#input1').value=res.items[0]['full_name'];
$w('#input2').value=res.items[0]['academic_email'];
$w('#input4').value=res.items[0]['country'];
$w('#input3').value=res.items[0]['team'];
}else{
$w('#input2').value='not working';
}
});
});
Users who has not logged in will be prompted to logged in when the visit the update page in order for us to retrieve their email and query the database
This is the actual publish site that would not prepopulate the fields based on the custom code