Issue with Pre-populating form inputs

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

This is usually a permission issue. Make sure you have set correct permissions on the dataset.

I have already set the correct permissions on both the database collection as well as the page. But it still doesn’t seem to work! :confused:

Are you sure you have synced the db to Live?

@giri-zano Yes, I always ensure that my db is synced to live. If you are willing, you can try out the code that I have written on a dummy site probably it will help to better troubleshoot the problem…? If the problem still persists I think the developers of Wix really need to look into this…

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

Hi Yisrael,
Thanks for referring me to the article. I finally realise that it is running asynchronously and therefore have made the following changes:

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 => {

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

});

However I still couldn’t manage to get it to work and I have also tried using the ‘await’ syntax that you have provided but the editor is throwing me an error as it doesn’t recognise it. Hope that this issue is able to get resolved soon! :confused:

@tcheajiawei What message are you getting? What is it doing?

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?