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';
}
});
});
![](https://static.wixstatic.com/media/a27d24_6ffd339e3d4b4eaf8c33a676a4021640~mv2.png/v1/fill/w_48,h_26,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/a27d24_6ffd339e3d4b4eaf8c33a676a4021640~mv2.png)
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 http://wix.to/94BuAAs
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';
}
});
});
![](https://static.wixstatic.com/media/a27d24_1e83efec77e54e5e8db1e5ea705a01d7~mv2.png/v1/fill/w_49,h_27,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/a27d24_1e83efec77e54e5e8db1e5ea705a01d7~mv2.png)
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
![](https://static.wixstatic.com/media/a27d24_87b861539ef945a6a21db65f37715ae5~mv2.png/v1/fill/w_74,h_53,al_c,q_85,usm_0.66_1.00_0.01,blur_2,enc_auto/a27d24_87b861539ef945a6a21db65f37715ae5~mv2.png)
This is the actual publish site that would not prepopulate the fields based on the custom code
Similar issue: I am able to get the inputs to pre-populate, but on submissions its blank. NOW, if the user adds any letter to the pre-populated field, it works. Whats happening?
Hi Jiawei,
You are getting the user email like this:
user.getEmail() .then((email) => { userEmail = email; // "davidlim@hotmail.com" });
After this you do a query, but the user Email is not yet available. The reason for this is that that the user.getEmail() function is asynchronous and the result returned in the .then() which is the fulfillment of the Promise.
You need to perform your query inside the .then() function. Or you can use await for the getEmail() call and then run your query. Something like this:
userEmail = await user.getEmail(); wixData.query( ... blah blah blah ...
For more information on Promises, see the article Working with Promises in Wix Code.
I hope this helps,
Yisrael
This is usually a permission issue. Make sure you have set correct permissions on the dataset.