get email instead an id

hi i´m trying to change my query to search for the same email login instead of the id of my database how can i do it

i have this code

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("_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 ?

thanks a lot

Hi,

This is how you search by the email field:


.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.

Cheers,

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 ?

i change my code for these and stil not working

it appears these problemens


my code

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( ( ***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

my dynamic page name is

database-trade/Update/{ID}

my url link
www.lupeweb.com

Hi Duda,

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.

Thank you,

i`ve got a problem
I’ve made again the code and still having problem

when click on MEU PAINEL nothing is done

here is my link

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", 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});
}

i really don’t know how to do

can you help me

Hi Duda,

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.

Liran.

I THINK THE PROBLEM MIGHT BE IN THIS CODE

wixLocation.to (/database-trade/Update/${wixUsers.currentUser.id}); <------ i think the problem is here

my dynamic page name is

database-trade/Update/{ID}

IT NOT CONNECTS TO THE CORRECT DYNAMIC PAGE ID

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);
} );
}
}

what need to be changed please help