This is driving crazy. I’m just trying to create a box where I type a text, I click on a button, and a related text from a database shows up. It’s been impossible after so many hours.
Can anybody please help?
My database is in Sandbox live and has 2 columns with thousands of raws. I would like the user to just type any text of column 1, press a button, and the corresponding text of column 2 shows up.
The name of my dataset is ‘dataset’ , the name of column 1 ‘planet’ and the name of column 2 is ‘esi’
You should your database instead of dataset…
#input1 - input where user types
#button1 - the button to be pressed
#text1 - the text where the result should be shown…
$w.onReady(function () {
$w('#button1').onClick((event) => {
wixData.query("DatabaseId") //Database
.contains("planet", $w('#input1').value)
.find()
.then((res) => {
if(res.items.length > 0) {
let items = res.items;
let field2 = items[0].esi;
$w('#text1').text = field2.toString();
} else {
// handle case where no matching items found
}
});
});
});
For more reference → wixData.query()
Thank you,
Where should I put that code?
And why ‘onClick (( event )’? my database is a table in the content manager.
I created an input box and linked it to ‘input’ but i cannot even write in that box.
When I create the button, the option ‘search data’ cannot be pressed. The button doesn’t work.
And when I create a small dinamic window where the text1 is supposed to appear, nothing appear.
Also, I get this error The dataset1 collection does not exist. You cannot work with a collection using the Data API before it is created in the Editor.
For this I already said - "You should use your database’s id not dataset’s
Thank you.
The problem is that when I connect input1 to the database, I cannot enter any input in the box later.
For the code to work, you should not connect any of the elements to the dataset…
The code →
$w.onReady(function () {
$w('#button1').onClick((event) => { //this is the button to be clicked for searching-
//the database
wixData.query("DatabaseId") //Database
.contains("planet", $w('#input1').value)
.find()
.then((res) => {
if(res.items.length > 0) {
let items = res.items;
let field2 = items[0].esi;
$w('#text1').text = field2.toString(); // #text1 is the text where the result
//is shown
} else {
$w('#text1').text = "No matches"; //if there is no matching result
}
});
});
});