SOLVED: Get individual field from database using user input

I am trying to use a user input dropdown populated by a database to get information back out of the database to populate a text box.

So, I have a drop down of ‘clientNames’ and would like the ‘address’ for the client to fill a text box.

I know this is probably pretty simple but I cannot figure it out.

Thanks!

Hi,
You need to use query to retrieve the data from the database, check it out here .

1 Like

I have tried that and it seems that the data retrieved can only be displayed in a table. Probably because the query returns a string of data. I’m only interested in a single field, not the whole string.

export function clientName_change(event, $w) {
wixData.query(‘ClientProfile’)
// Query the collection for any items whose “clientName” field contains
// the value the user selected in the dropdown
.contains(‘clientName’, $w(‘#clientName’).value)
.find() // Run the query
.then(res => {
// Set the table data to be the results of the query
$w(‘#table1’).rows = res.items;
$w(‘#table1’).show();
});
}

Instead of these rows which display the data in a table,

$w('#table1').rows = res.items;
$w('#table1').show();

You can use the data however you want, you can take any field and set it as a text’s value, for example:

let data = res.items[0].text;//here I save the value of the field I want to use
$w('#text1').text = data;

Good luck :slight_smile:

1 Like

That worked perfectly! Thanks.