export function loginButton_click_1(event) {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w(“#loginButton”).label = “Login”;
$w(“#profileButton”).hide();
} );
}
// user is logged out
else {
let userId;
let userEmail;
// 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("database_trade")
.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
};
// add the item to the collection
wixData.insert("database_trade", toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w("#loginButton").label = "Logout";
$w("#profileButton").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function profileButton_click(event, $w) {
wixLocation.to(/database-trade/Update/${wixUsers.currentUser.id});
}
where i need to change to search for an email instead of a ID ?
.then( (user) => {
return user.getEmail();
} )
.then( (email) => {
// check if there is an item for the user in the collection
userEmail = email;
return wixData.query("database_trade")
.eq("email", userEmail)
.find();
} )
I hope it was helpful. If not, please give me more details on your question and maybe attach your site url.
hi
my code is this then or i need to delete something ?
import wixUsers from ‘wix-users’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;
$w.onReady( () => {
if(wixUsers.currentUser.loggedIn) {
$w(“#loginButton”).label = “Logout”;
$w(“#profileButton”).show();
}
else {
$w(“#loginButton”).label = “Login”;
$w(“#profileButton”).hide();
}
} );
export function loginButton_click_1(event) {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w(“#loginButton”).label = “Login”;
$w(“#profileButton”).hide();
} );
}
// user is logged out
else {
let userId;
let userEmail;
// 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(“database_trade”)
.eq(“email”, user.getEmail( ) ) <------------------ ONLY CHANGE THESE ?
.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
};
// add the item to the collection
wixData.insert(“database_trade”, toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w(“#loginButton”).label = “Logout”;
$w(“#profileButton”).show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function profileButton_click(event, $w) { wixLocation.to (/database-trade/Update/${ [wixUsers.currentUser.id](http://wixusers.currentuser.id/) }); <--------- MUST CHANGE ?
} <----------- I NEED DO DELETE THESE ONE OR NOT ?
export function loginButton_click_1(event) {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w(“#loginButton”).label = “Login”;
$w(“#profileButton”).hide();
} );
}
// user is logged out
else {
let userId;
let userEmail;
// prompt the user to log in
wixUsers.promptLogin( {"mode": "login"} )
.then( (user) => {
userId = user.id;
return user.getEmail();
} )
.then( ( ***user*** ) => {
// check if there is an item for the user in the collection
userEmail = email; <----- IT APPEARS A PROBLEM HERE
return wixData.query("database_trade")
.eq(" ***email*** ", ***user.getEmail ( )*** )
.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
};
// add the item to the collection
wixData.insert("database_trade", toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w("#loginButton").label = "Logout";
$w("#profileButton").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function profileButton_click(event, $w) {
wixLocation.to(/database-trade/Update/${wixUsers.currentUser.email}); <------ i think the problem is here
Have a look at my last comment. I changed the code a little bit and maybe now it will work.
Moreover, copying all of your code to the forum doesn’t help me.
If the code I added didn’t work out for you => please try to focus the problem for me.
export function loginButton_click_1(event) {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixUsers.logout()
.then( () => {
// update buttons accordingly
$w(“#loginButton”).label = “Login”;
$w(“#profileButton”).hide();
} );
}
// user is logged out
else {
let userId;
let userEmail;
// 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("database_trade")
.eq("email", userEmail)
.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
};
// add the item to the collection
wixData.insert("database_trade", toInsert)
.catch( (err) => {
console.log(err);
} );
}
// update buttons accordingly
$w("#loginButton").label = "Logout";
$w("#profileButton").show();
} )
.catch( (err) => {
console.log(err);
} );
}
}
export function profileButton_click_1(event, $w) {
wixLocation.to(/Database_trade/Update/${wixUsers.currentUser.email});
}
We would really like to help you, but it is very hard for us to debug a long code.
Please try to focus your problem to a few lines of code, and if you have any error in the browser console, please add it.
import wixUsers from ‘wix-users’; import wixData from ‘wix-data’; import wixLocation from ‘wix-location’;
export function button3_click(event, $w)
{ if ( $w(“#group1”).hidden ) {
$w(“#group1”).show();
} else {
$w(“#group1”).hide();
}
//Add your code for this event here:
}
export function button1_click(event, $w)
{ if ( $w(“#group1”).hidden ) {
$w(“#group1”).show();
} else {
$w(“#group1”).hide();
}
} let user = wixUsers.currentUser;
let userId = user.id; // “r5cme-6fem-485j-djre-4844c49” let isLoggedIn = user.loggedIn; // true let userRole = user.role; let userEmail; // “Member”
user.getEmail()
.then( (email) => { let userEmail = email; // “user@something.com”
} ).then( (user) => { return user.getEmail();
} )
.then( (email) => {
// check if there is an item for the user in the collection
userEmail = email; return wixData.query(“Profile”)
.eq(“email”, userEmail)
.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
};
// add the item to the collection
wixData.insert(“Profile”, toInsert)
. catch ( (err) => {
console.log(err);
} );
}
}