I have a basic function to grab the email address of the logged in user:
import wixUsers from 'wix-users';
var myUser = wixUsers.currentUser;
var userEmail;
$w.onReady(function () {
myUser.getEmail()
.then( (email) => {
userEmail = email; // "user@something.com"
console.log(userEmail);
} );
});
The logged in user email currently appears in the console correctly. What I want to do now is get that email address and put it into a user input box (or similar, such as drop-down, whatever is easier or makes more sense). How can I do this?
The goal is to then be able to filter a Database by the value (email) found in that input box, thus allowing me to only show info to a user IF the email in the aforementioned input box matches the email field in the database.
Hope this makes sense…
Any help is very much appreciated!
Thanks,
Luke
#FilterDatasetByLoggedInEmail #DatasetFilters #FilterDatasetWithEmail #DatasetFiltersEmail
How about:
import wixUsers from 'wix-users';
var myUser = wixUsers.currentUser;
var userEmail;
$w.onReady(function () {
myUser.getEmail()
.then( (email) => {
userEmail = email; // "user@something.com"
console.log(userEmail);
$w('#someInput').value = userEmail;
} );
});
where ‘someInput’ would be the name for an input Element?
And for your next question: “the email I just set on the form is not saved into the Collection”, please don’t ask. When using datasets, just look at setFieldValue().
Thank you, this works for most inputs!
I was struggling to get this working for a dropdown (which is what Wix requires when filtering a database by a user input field). So I searched the Velo references and found how to set the values of a dropdown with code. See the Velo references here.
My final code to get this working:
import wixUsers from 'wix-users';
var myUser = wixUsers.currentUser;
var userEmail;
$w.onReady(function () {
myUser.getEmail()
.then( (email) => {
userEmail = email; // "user@something.com"
console.log(userEmail);
$w("#dropdown1").options = [
{"label": userEmail, "value": userEmail} // sets the label/value of dropdown
];
} );
});
Not sure if it is important, but in the settings of the dropdown I had the “Show Initial Text” set to “Item from dropdown” (which in this case would only be 1 item). I also deleted all of the default “choices” from the dropdown. Screenshot:
Anyway, now I can filter my dataset through this dropdown so that only info that belongs to the current logged in user (by email) is shown. Here’s the dataset filter I used:
Everything works perfectly!
Thanks for your help @Giri Zano !
-Luke