Auto Select Reference Item Dropdown

Sorry for my bad English :wink:
Thanks to anyone who can help me.

I have a table (B) attached to a table (A) by the “email” reference field.

In a dynamic page I have an input form (just write) with the fields in the table (B).

The “email” entry is a table reference (A), so it’s a Dropdwon item in the form.

Let’s ask the question:
How to automatically select in Dropdown the user login email? As it is logged in, it does not make sense to ask you to fill in this field.

I am not a programmer, I am enthusiastic, so I thank anyone who comes to help me with codes;)

Thank you all for the time taken for this moment.

Hello

Check if the user is logged in then get the user email, After that you set the dropdown value to the email:

import wixUsers from 'wix-users';

// ...

let user = wixUsers.currentUser;

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

if(isLoggedIn){
user.getEmail()
  .then( (email) => {
    let userEmail = email;      // "user@something.com"
    $w('#dropdown1').value = userEmail
  } );
}  

Note that the email should be one of the dropdown options.

Best
Massa

Hi Massa, thanks for the time dedicated in your reply.

Your code works perfectly when the data set is just read or read and write. However the form is data inclusion, when I change the data set to “just write” the dropdwon gets null.

Do you have any suggestion of code or configuration to solve this?

thankful

It gets null because it can’t access the data set anymore so it can’t read the values.
If you need to keep the dataset write only then you have to set the dropdown options using code and not by connecting it to the dataset:
to do that you query the collection for the field with the drop down options → you will get an array of all the values in that field
then you map this array to get the result in dropdown options array format ([{label,value}]) and you set the dropdown options to it:

$w("#myDropdown").options = [
  {"label": "Who's on first!", "value": "first"},
  {"label": "What's on second", "value": "second"},
  {"label": "I Don't Know is on third", "value": "third"}
];

Good luck
Massa

Simple as it may seem, my little knowledge prevents me from doing :frowning:

I have two databases:
A: User profile

  • Email
  • Name

B: Question

  • Email (with reference to the database The email item)
  • Answer (question answer)

Database A (user profile) I only have one row per user. Database B (Question) I have several lines per user related to Bank A, that is, it can respond several times differently.

In the form page I have:

  • Email (automatically populated)
  • Answer
  • Send button

Could you please write the code to be inserted on the page?

Hello Raoni

would you please provide you editors link so i could understand how exactly your website works.
then I will guide through the steps you need to do to achieve your goal :slight_smile:

however note that this forum is specialized in wix code problems and helping you solve it and not writing the full code. you can always visit wix arena there’s plenty of wix experts ready to design and create your website for you!

Best
Massa