Portion of code doesn't appear to be running (with no perceivable explanation). Would love some help with this...

So I’ve persevered and I’m actually SO close to solving the issue I’ve had with populating text elements after form submission…

It seems as though the correct item is now being retrieved from the database but not all of the text elements are being populated:


The blue circle shows the correct data that has been successfully set as the element’s text. The red circles show the text elements that have not been set and remained as the example text created within the editor - As you can see, only 4 out of the 14 text elements have been successfully populated using the data retrieved by the code.

Here is the relevant piece of code I am using to retrieve the data item and display it in the text elements:

function populateRegConfirmation() {
 
 if (($w("#programSelectDropdown").value==='Soccer')) {
 const dobOptions = {day: "2-digit", month: "2-digit", year: "numeric"};
 // Get current user.
 let user = wixUsers.currentUser;
 let userId = user.id;
    user.getEmail()
    .then((email) => {
 let userEmail = email; // "user@something.com"
        console.log(userEmail);
        console.log(userId);
 // Filter dataset to show items created by current user.
 // get last item by current user.
    wixData.query("soccerMembersDatabase")
    .eq("email", userEmail)
    .find()
    .then( (results) => {
 if(results.items.length > 0) {
 let details = results.items[0];
                $w("#name").text = details.firstName + " " + details.lastName;
                $w("#phone").text = details.phone;
                $w("#email").text = details.email;
                $w("#address").text = details.address + ", " + details.city + ", " + details.postCode;
                $w("#dob").text = details.birthDate([], dobOptions).toDateString;
                $w("#gender").text = details.gender;
                $w("#ecName").text = details.emergencyContactName;
                $w("#ecPhone").text = details.emergencyContactPhone;
                $w("#ecRelationship").text = details.emergencyContactRelationship;
                $w("#goalieStatus").text = details.goalkeeper;
                $w("#skillLevel").text = details.skillLevel;
                $w("#regRefNo").text = details.id;
                $w("#program").text = details.program.toString();
                $w("#totalDue").text = details.paymentDue;
 
        } else {
 // handle case where no matching items found
        }
    } )
    .catch( (err) => {
 let errorMsg = err;
    } );
    })
 
    }
}

I am not getting any errors within the editor coding panel or in the preview console.

I have no idea why it would be successfully running the first 4 lines of code within “if(results.items.length > 0)” but not any others after that.

I’d really appreciate any help with this mystery!

Console.log the “results” in the then() and the "err’ in he catch()

Thank you @mvveiga .

Okay so the error message coming up now is:

" TypeError: o.birthDate is not a function
at by75g.js:380 "

I’m guessing this is relating this line of code:

 $w("#dob").text = details.birthDate([], dobOptions).toDateString;

which would make sense because that’s the point where the code stops running.

But I’m not sure what is wrong with that as I’m sure that was working correctly before…

EDIT: I’ve just realized it was because I placed the () after “birthDate” which isn’t a function so I’ve changed it to this:

 $w("#dob").text = details.birthDate.toDateString(dobOptions);

Which has fixed the original issue! But now the (dobOptions) isn’t working properly… This is how I’ve set the dobOptions variable:

const dobOptions = {day: "2-digit", month: "2-digit", year: "numeric"};

I want the dob to show like this “31/01/2020”

Check the intl object.
Intl.DateTimeFormat() constructor - JavaScript | MDN.

1 Like

Thanks, I’ve figured out that I was simply missing “Locale” from the conversion to date string. So it should’ve been “.toLocaleDateString” instead of “.toDateString”.
(Woops!)

1 Like

@mvveiga I wonder if you could also help me out with the following…

function registerUser() {
 // Get user details for register or log in.
 let phone = $w("#phoneInput").value;
 let lastFourDigits = phone.substring(phone.length - 4, phone.length);     //substring containing last 4 characters
        console.log("Last 4 phone digits:",lastFourDigits);
 let email = $w("#emailInput").value;
 let password = "GBSC" + lastFourDigits;
 let firstName = $w("#firstNameInput").value;
 let lastName = $w("#lastNameInput").value;
 // Find if user has previously registered; if registered: log in, if not yet registered: register and then log in.
    wixData.query('soccerMembersDatabase')
      .eq('email', email)
      .find()
      .then((results) => {
 if(results) {
            wixUsers.login(email, password)
                .then( () => {
                    console.log("Existing user is logged in");
                } )
                .catch( (err) => {
                    console.log(err);
                } );
        } else {
            wixUsers.register(email, password, {
                contactInfo: {
 "firstName": firstName,
 "lastName": lastName,
 "phones": [phone]
                }
            } )
            .then( (result) => {
 let resultStatus = result.status;

                wixUsers.login(email, password)
                .then( () => {
                    console.log("New user is logged in");
                } )
                .catch( (err) => {
                    console.log(err);
                } );
            } );
          }
      })

}

This is supposed to log in the user if their email already exists within the database or register them and then log them in if their email is not in the database.

It works for users that are already in the database but not for users that aren’t.