@aeroengineerz7 ahhh!!! Glad a video help you out!!
Alright — so let’s look at your code then. It’s just a matter of putting things in the right place so that the logic is correct.
You have a password on keypress, but then you also have a register button on click event but they are doing 2 different things and neither of them have the Owner ID saved into the database.
At the beginning of your code you have a redirect to a dynamic page with the user ID. This code will not work if you did not capture the user ID during the registration.
So lets add the missing logic to make sure our code captures the missing information and triggers things in the correct order. I added more notes on a few lines.
(Notice that I left out the password in the database insert, as this is not good practice to save user’s credentials. P.s. Sharing your entire code vs partial code helps the community understand the full picture when helping someone to troubleshoot.)
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
import wixWindow from 'wix-window';
import wixData from 'wix-data';
let registration;
$w.onReady(function () {
if (wixUsers.currentUser.loggedIn) {
wixLocation.to(`/member/my-profile/${wixUsers.currentUser.id}`); //Change the URL ending to the page you want to redirect the user if they are already logged in
} else {
console.log("User was not logged in when they arrived here.")
}
$w("#password").onKeyPress((event) => {
let key = event.key;
$w('#errorMessage').hide();
$w('#emailExists').hide();
if (key === "Enter") {
console.log("Pressed Enter key on Password field"); //You can change the text of this line or delete it
if ($w("#email").valid && $w("#password").valid && $w("#firstName").valid && $w("#lastName").valid) {
//registerPerson(); //You can delete all the other lines and simply trigger the registerPerson function so that you have less code here
let email = $w("#email").value;
let password = $w("#password").value;
let first = $w("#firstName").value;
let last = $w("#lastName").value;
wixUsers.register(email, password, {
contactInfo: {
"firstName": first,
"lastName": last
}
})
.then((user) => {
let userId = user.id; //after the person is registered we get their user ID
const toInsert = {
"emailAddress": email,
"firstName": first,
"lastName": last,
"_id": userId,
};
// add the item to the collection
wixData.insert("MemberPage", toInsert)
.then(() => {
$w("#successful").show();
wixLocation.to("/member/welcome-new-user");
})
.catch((err) => {
let errMsg = err;
console.log("A record with a matching ID already exists, or user does not have permission to insert into this collection.");
});
})
.catch((err) => {
let errorMsg = err;
console.log(errorMsg);
$w('#emailExists').show();
});
} else {
console.log("Some inputs are invalid"); //You can change the text of this line or delete it
$w('#errorMessage').show();
}
} else {
console.log("Did not press Enter key."); //You can change the text of this line or delete it
}
});
$w("#registrationButton").onClick((event) => {
console.log("Button was clicked"); //You can change the text of this line or delete it
$w('#errorMessage').hide(); //We want to hide all error messages when the person attempts to register again
$w('#emailExists').hide(); //We want to hide all error messages when the person attempts to register again
if ($w("#email").valid && $w("#password").valid && $w("#firstName").valid && $w("#lastName").valid) {
registerPerson();
console.log("Trying to register"); //You can change the text of this line or delete it
} else {
$w('#errorMessage').show(); //This is were we prompt the message to show up again ONLY if there is an error
console.log("Missing information"); //You can change the text of this line or delete it
}
});
});
function registerPerson() {
let email = $w("#email").value;
let password = $w("#password").value;
let first = $w("#firstName").value;
let last = $w("#lastName").value;
wixUsers.register(email, password, {
contactInfo: {
"firstName": first,
"lastName": last
}
})
.then((user) => {
let userId = user.id; //after the person is registered we get their user ID
const toInsert = {
"emailAddress": email,
"firstName": first,
"lastName": last,
"_id": userId, //yes, the _id and the _owner can be the same value
};
// add the item to the collection
wixData.insert("MemberPage", toInsert)
.then(() => {
$w("#successful").show();
wixLocation.to("/member/welcome-new-user");
})
.catch((err) => {
let errMsg = err;
console.log("A record with a matching ID already exists, or user does not have permission to insert into this collection.");
});
})
.catch((err) => {
let errorMsg = err;
console.log(errorMsg);
$w('#emailExists').show();
});
}