I’m having a little trouble with something that I’m trying to do.
I currently have a database containing a list of vehicles owned by my site’s members. I will be managing and updating the data personally, so all records have my user id in the Owner field.
I would like to have a list in the members area of my site which shows a user all of THEIR vehicles, however because I am the owner of all the records I can’t simply filter by Logged in User on the Owner column.
My solution to this was to have an email address column in the database and filter by that, however my attempts at fetching the currently logged in user’s email all failed - I’m not sure if I understand how to use the APIs. If somebody could please explain the process to get the user’s email address and then use that address to filter a dataset, that would be extremely helpful.
Alternatively, is there another way that I could go about filtering these results? I’m open to any ideas.
import wixUsers from 'wix-users';
wixUsers.currentUser.getEmail()
.then( (email) => {
let userEmail = email; // "user@something.com"
} );
I suggest you to add this method to the onReady() function of the member area page. This function runs before the user starts interacting with your page.
If you still have a problem, please send us a URL to your site and specify the name of the page.
Hi Sapir, thanks very much for your reply. I swear that I had tried that exact code previously and kept getting an error about an unhandled promise in the console, but when I typed it in this time it seemed to work fine.
Now that I think I have the email address, I’m trying to filter my dataset with the code below, but that doesn’t seem to be working. clearly I’m missing something.
import wixUsers from ‘wix-users’; import wixData from ‘wix-data’;
$w.onReady( function () {
//TODO: write your page related code here…
wixUsers.currentUser.getEmail()
.then( (email) => { let userEmail = email; //"user@something.com"
})
Please feel free to ignore my previous post, as I figured out where I was going wrong. I solved my problem and now have the dataset filtering correctly with the code below:
import wixUsers from 'wix-users';
import wixData from 'wix-data';
$w.onReady(function () {
//Get current user email address
wixUsers.currentUser.getEmail()
.then( (email) => {
let userEmail = email; //"user@something.com"
//Filter the dataset
$w("#myVehiclesSet").setFilter(wixData.filter()
.eq("vehicle_user", email))
})
});
Because this function is inside the onReady() function after the page is loaded the input element in the form will be filled in the current user’s email , so now he will only have to fill the rest of the inputs.
BUMP Any update with this i have a similar issue and currently have the following code but does not return the right values from the database just the first row of values…
(email addresses in my database are under the column ‘Client’)
import wixUsers from ‘wix-users’; import wixData from ‘wix-data’;
$w.onReady( function () {
//Get current user email address
wixUsers.currentUser.getEmail()
.then( (email) => { let userEmail = email; //"user@something.com"
//Filter the dataset
$w(“#dataset1”).setFilter(wixData.filter()
.eq(“Client”, email))
})
Sapir, this worked great for me to filter by email…THANK YOU! However, I now want to filter data to the Company associated with the currently logged in user. I do ask for Company on my custom member sign up form. However, I don’t see the Company field from the custom form in my Private Members Data collection, so I can’t figure out how to filter on that field. Can you help?
Hi! I have code that is doing a good job of matching the email address of the currently logged in user to the “submittedByEmail” in my collection. However, I would prefer to filter by the Domain of the currently logged in user, which I have as a column in my dataset as “submittedByDomain”. Do you how I would alter the code so that it shows the data for the currently logged in user that matches the “submittedByDomain” of that user in my dataset?
import wixUsers from ‘wix-users’; import wixData from ‘wix-data’;
$w.onReady( async function () { let userEmail = await wixUsers.currentUser.getEmail()
$w(“#dataset1”).onReady(() => {
$w(“#dataset1”).setFilter(wixData.filter()
.eq(“submittedByEMail”, userEmail)
)
});
I’m essentially doing a similar thing here but running into issues.
I need to create some sort of query based on the currentUser and run it against a second dataset on the page.
I need to write the email (currentUser) and the first name and last name from the first data set (members) to the second dataset if they don’t already exist there and in page’s form allow the user to edit their other details in the second data set. Sapir or others is there a way you could assist me?