Help with Database Query..please!

I have some coding that I need help with, I get very discouraged with queries since I can’t figure out the logic to actually write them. I know exactly what I want it to do but don’t know how to write it. :frowning:

Currently, when the button is clicked, it gets the users email address from Wix Data and directs them to their profile page. This is my active code:

export function profileButton_click_1() {
 
let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true
let userRole = user.role;       // "Member"

user.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
    wixLocation.to("http://www.stlmini.com/MembershipDatabase/"+userEmail);
  } );
}

I need it so that significant others with access to the members only site can view the profile as well, their address is on the same line in the same database.

What I would like it to do after let userEmail = email; is to query MembershipDatabase:
If userEmail has a match in email,
then wixLocation.to(“http://www.stlmini.com/MembershipDatabase/”+userEmail);
If userEmail does not have match in email,
then look up userEmail in soEmail and pull back primary member email on same row
and wixLocation.to(“http://www.stlmini.com/MembershipDatabase/”+email //identified in query);

email = the field setting for the primary member email address
soEmail = significant other email address
MembershipDatabase = database for member information
I’d like for this all to be done on the profileButton_Click_1 command.

Thanks in advance!

export function checkEmail(email) {
 return wixData.query("MembershipDatabase")
 .eq("emailField", email)
 .limit(1)
 .find().
 .then((results) => {
   if (results.totalCount > 0) {
     return true;
   }
   return false;
 })
}
export function checkEmailSO(email) {
 return wixData.query("soEmail")
 .eq("emailField", email)
 .limit(1)
 .find().
 .then((results) => {
   if (results.totalCount > 0) {
     return true;
   }
   return false;
 })
}

So after adding the two above…

let userEmail = email;
let doesitExistInMembership = checkEmail(userEmail);
if (doesitExistInMembership) {
  // It does
} else {
  // It does not
  let doesitExistInSO = checkEmailSO(userEmail);
  if (doesitExistInSO) { 
    // It does in SO
  } else { 
    // It does not in SO
  }
}

Rock N Roll!

1 Like

Thanks for your feedback. But I’m a bit confused.

So I should add the two next export functions above the profile button click function. Where would I add the let user email to get the email to appear in the url. Would it be like this?

export function checkEmail(email) {
 return wixData.query("MembershipDatabase")
 .eq("emailField", email)
 .limit(1)
 .find().
 .then((results) => {
   if (results.totalCount > 0) {
     return true;
   }
   return false;
 })
}

export function checkEmailSO(email) {
 return wixData.query("soEmail")
 .eq("emailField", email)
 .limit(1)
 .find().
 .then((results) => {
   if (results.totalCount > 0) {
     return true;
   }
   return false;
 })
}

export function profileButton_click_1() {
 
let user = wixUsers.currentUser;

let userId = user.id;           // "r5cme-6fem-485j-djre-4844c49"
let isLoggedIn = user.loggedIn; // true
let userRole = user.role;       // "Member"


user.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
  let doesitExistInMembership = checkEmail(userEmail);
  if (doesitExistInMembership) {
  // It does
  } else {
  // It does not
  let doesitExistInSO = checkEmailSO(userEmail);
  if (doesitExistInSO) { 
    // It does in SO
  } else { 
    // It does not in SO
  
    wixLocation.to("http://www.stlmini.com/MembershipDatabase/"+userEmail);
  } );
}

Does that not work you mean? It looks good.

@andreas-kviby There’s a lot more coding on the page to show/hide buttons based on a user being logged in, when I add that code, everything is visible and the profile button doesn’t work.