Use wixData.query results as text

I have written a query to retrieve results from my dataset, (“Postcodes”), where the field “postcodePrefix” matches the user’s entry in “#pcinput”.

I am only expecting one result from the query and would like to use the value from the “classQ” field in the dataset within a text box in #text1. My code is as follows:

export function CheckPC(event) {
//Add your code for this event here:
var pc = $w(“#pcinput”).value.substring(0,2);
pc = pc.replace(/[0-9]/g, ‘’);

// Runs a query on the “recipes” collection
wixData.query(“Postcodes”)
// Query the collection for any items whose “Name” field contains
// the value the user entered in the input element
.eq(“postcodePrefix”, pc)
.find() // Run the query
.then(res => {
//what goes here?
});
}

I need to understand what follows .then(res => { in order that I can use the value as a string.

If you need only one result, go ahead and add .limit(1) to your query.

$w(‘#text1’).text = res.items[0].classQ;

1 Like

Since you only need a single value, it’ll be even easier to use distinct() :

wixData.query("Postcodes")  
.eq("postcodePrefix", pc)
.distinct("classQ")
.then((res) => {
$w("#text1").text =  res.items[0];
})
1 Like