I've created a custom sign-up form that I would like to insert data into a database from, and then log the user in automatically. The problem is that I cannot seem to insert the data into the database before the log-in process starts. Here is the current code:
import wixUsers from 'wix-users';
import wixWindow from 'wix-window';
import wixLocation from 'wix-location';
import wixData from 'wix-data';
import cryptoJS from 'crypto-js';
import base64 from 'nodejs-base64-encode';
$w.onReady(function () {
//TODO: write your page related code here...
$w("#signup").onClick(async (event) => {
let username = $w("#username").value;
let email = $w("#email").value;
let password = $w("#password").value;
await wixData.query("PlayerData")
.eq("email", email)
.find()
.then((results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// create an item
let toInsert = {
"title": username,
"email": email,
"password": password
};
// add the item to the collection
return wixData.save("PlayerData", toInsert);
}
});
await wixUsers.register(email, password, {
contactInfo: {
"slug": $w('#username').value,
"nickname": $w('#username').value,
"name": $w('#username').value
}
})
.then((results) => {
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;
let userId;
});
});
});
As it turns out, it is not necessary to run the registration or login portion of the code in the OP. Because, the sign-up button on a custom sign-up form already performs this action. I was able to get the desired result using the following lines of code:
import wixUsers from 'wix-users'; import wixWindow from 'wix-window'; import wixLocation from 'wix-location'; import wixData from 'wix-data'; $w.onReady(function () { $w("#signup").onClick((event) => { let username = $w("#username").value; let email = $w("#email").value; let password = $w("#password").value; return wixData.query("PlayerData") .eq("email", email) .find() .then((results) => { if (results.items.length === 0) { let toInsert = { "title": username, "email": email, "password": password }; return wixData.insert("PlayerData", toInsert); } }); }); });
Also import those API's which you imported into your code.
don't use this
return wixData.save("PlayerData", toInsert);
use this one insert any new data into your 'PlayerData' database.
return wixData.insert("PlayerData", toInsert);