I’ve looked all over and have found many articles/Q&A answering my question that are similiar but not exactly what I’m looking for. I think the answer is right in front of my face but I can’t seem to get it… Trying to explain this as easy as I can… A New Member signs up on my website, (that user has a unique Wix Member ID). How do I get each new member’s unique Wix ID to automatically be added to my database? Im creating private dynamic member pages based on the Wix Member ID but can’t figure this part out. Thank you in advance for your time and any information is very much appreciated.
Dear :
i use this code
you need to sign out and sign in to let the code check your id
this will check the ID then insert it to the collection if is not in the collection
it’s will check the field number “acnumber” in Members
acnumber = 1000
randf = 1000+1
and it will add title “student1001”
I hope this will help you
import wixData from 'wix-data';
import wixLocation from 'wix-location';
import wixUsers from 'wix-users';
$w.onReady( () => {
user = wixUsers.currentUser;
userId = user.id; // "r5cme-6fem-485j-djre-4844c49"
user.getEmail()
.then( (email) => {
userEmail = email; // "user@something.com"
} );
wixData.query("Members")
.descending("title")
.find()
.then( (results) => {
rand = results.items[0]["acnumber"];
randf = `${Number(rand) + 1}`;
addrand = ("student"+randf);
} );
});
export function button1_click() {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixData.query("Members")
.eq("_id", userId)
.find()
.then((results) => {
let prrcon = results.items[0]["pr"];
// if an item for the user is not found
if (prrcon === "superadmin"){
wixLocation.to('/supervisor');
}
else if(prrcon === "admin") {
wixLocation.to('/supervisor');
}
else if (prrcon === "subadmin" ) {
wixLocation.to('/supervisor');
}
else if (prrcon === "teacher" ) {
wixLocation.to('/stu-tsmya');
}else {
$w('#text44').show();
}
});
}
// user is logged out
else {
// prompt the user to log in
wixUsers.promptLogin( {"mode": "login"} )
.then( (user) => {
userId = user.id;
return user.getEmail();
} )
.then( (email) => {
// check if there is an item for the user in the collection
userEmail = email;
return wixData.query("Members")
.eq("_id", userId)
.find();
} )
.then( (results) => {
// if an item for the user is not found
if (results.items.length === 0) {
// create an item
const toInsert = {
"_id": userId,
"email": userEmail,
"pr": normal,
"title": addrand,
"acnumber": randf
};
// add the item to the collection
wixData.insert("Members", toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w("#button1").label = "Go to control";
} )
.catch( (err) => {
console.log(err);
} );
}
}
Br
Hi all!
If we’re talking about the same thing, and you’re saving the members data correctly to the database, the ID is already generated.
simply press on the “Visible fields” tab in the database’s page and check the “ID” to show it.
How to use it?
For example, using the wix-user API you can get the current user’s ID and compare it to the IDs that are saved in your database.
If I got you wrong and you mean something else, please elaborate and I’d be glad to help further more.
Doron.
Hi Doron!
I tried what you said above, however, the ID is different for every submission from same logged in user. Is it possible to have each logged in user to have an ID or is the ID only for each entry?
Thank you!
Hi Jack!
In cases like this it is better to use a similar but different field called “_owner”.
You can find it the same way as the _ID and its purpose is to link between of all the items belongs to the same user who submitted/own them.
Check this article for further information.
Doron.
Hi Doron!
Thank you for your help! The _owner option works perfectly! Do you happen to know if I can pull the user’s email from their owner number? I’d like to be able to email them depending on the number/type of responses they submit into my database.
Thank you for the help!
- Jack
Hey Jack!
Sure ya can!
Check out our Wix-Users API .
It even has an example exactly for pulling the logged-in user’s Email.
Doron.
Hi Doron, I just came accros your comment above about ID’s and am hoping you can help me please? My question is, do I need to add ome kind of code to connect data that’s entered from a member on a page that’s connected in a data set but is somehow getting transferred to the data set but not corresponding to the right persons ID?
hello, on my site I have a “register” page which saves emails in a database, I have a my account page with a form on it which takes additional information about this user once he is connected, How do I add the additional data from the form next to the email of the member of my site in the database without asking for his email again (because he is already connected)?
I need to create a datbase that would contain the mark of my students and display their results using ID number.can anyone help me