How do I insert items into a collection upon the creation of an account?
Product:
Wix Editor
What are you trying to achieve:
I’m trying to set some default values for each user account that is created. As an example, I need the beginning “score” for each user to be 0.
What have you already tried:
I’m using a custom sign up page, and so I’m trying to do a submitButton.onClick() and have the things in there. This is what my code currently looks like:
$w.onReady(function () {
const initializeUser = () => {
//Gets the current userId
const currentUserId = wixUsers.currentUser.id;
const firstName = $w("#firstNameBox").value;
const lastName = $w("#lastNameBox").value;
const email = $w("#emailBox").value;
const password = $w("#passwordBox").value;
const username = $w("#usernameBox").value;
let toInsertCredentials = {
"userId": currentUserId,
"username": username,
"password": password,
"firstName": firstName,
"lastName": lastName,
"email": email
};
wixData.insert("#credentialsDataset", toInsertCredentials)
.then((item) => {
console.log(item);
})
.catch((err) => {
console.log(err);
});
//Create a new item in userValuesDataset and in userScoreDatase and gives each value a starting point
let toInsertScore = {
"_userId": currentUserId,
"score": 0,
"pointsFromWater": 0,
"pointsFromSteps": 0,
}
let toInsertDailyValues = {
"_userId": currentUserId,
"waterIntake": 0,
"steps": 0
}
wixData.insert("#userScoreDataset", toInsertScore)
.then((item) => {
console.log(item);
})
.catch((err) => {
console.log(err);
});
wixData.insert("#userDailyValuesDataset", toInsertDailyValues)
.then((item) => {
console.log(item);
})
.catch((err) => {
console.log(err);
});
}
$w("#submitButton").onClick(initializeUser);
});
However, when I run this, I get this error: Error: WDE0025: The #credentialsDataset collection does not exist. You cannot work with a collection using the Data API before it is created in the Editor.
I am very positive that those collections do exist, but it’s not accessing them correctly. I’m using the correct IDs, I know that too. My curiosity is this: do the collections not actually “exist” until a user account is created? Should I have this code run on some other page or something? If so, I am open to suggestions about it.