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;
});
});
});
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);
Thank you. However, this does not seem to resolve the problem. The data can be inserted if I leave out the log-in portion of the code. But the data insertion will not occur with it in place & appears to be firing before the data gets inserted.
@Magework Studios
$w.onReady(function () {
//TODO: write your page related code here...
$w("#signup").onClick((event) => {
let username = $w("#username").value;
let email = $w("#email").value;
let password = $w("#password").value;
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
wixUsers.register(email, password, {
contactInfo: { "slug": $w('#username').value,
"nickname": $w('#username').value,
"name": $w('#username').value }
})
.then((results) => {
wixData.insert("PlayerData", toInsert);
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn =user.loggedIn;
let userId;
});
});
});
Please use this code and inform whether it’s working or not
Also import those API’s which you imported into your code.
@umairboss26 I updated with the aforementioned code. The result is the same, in that login occurs before the data can be stored.
@mageworkstudios What are the permissions of your database?
@umairboss26 They are currently set to “Anyone”, as I had originally thought this might be the issue.
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);
}
});
});
});
Why you are comparing with your database when user is signing up for account
@umairboss26 This is to check whether the email already exists in the database