How to get data out of your DATABASE, how to filter it and how to use the results of the filtered data… ?..
Let us say you have a DATABAS-A in this DATABASE you have 5-COLUMNS full of DATA.
- You start querying the data of your DB… DB-NAME = “DATABASE-A”
import wixData from 'wix-data';
wixData.query("DATABASE-A")
.find()
.then( (results) => {
if(results.items.length > 0) {
let firstItem = results.items[0];
} else {
}
} )
.catch( (err) => {
let errorMsg = err;
} );
- When you input the following filter-command–> .contains(“column”, “value”)
you will get filtered DATA-RESULTS.
import wixData from 'wix-data';
wixData.query("DATABASE-A")
.contains("column-1", "value")
.find()
.then( (results) => {
if(results.items.length > 0) {
let firstItem = results.items[0];
} else {
}
} )
.catch( (err) => {
let errorMsg = err;
} );
Now you are able to filter the data by a valued seach-term (“value”) in a specific column of your DB.
You will get an OBJECT of RESULTS, where you can find all of the founded/filtered DATA…
.then( (results) => { }
Now you can check your results…
import wixData from 'wix-data';
wixData.query("DATABASE-A")
.contains("column-1", "value")
.find()
.then( (results) => {
if(results.items.length > 0) {
let myRESULTS = results
let myResultItems = results.items
let myFirstFoundItem = results.items[0].title
console.log(myRESULTS)
console.log(myResultItems)
console.log(myFirstFoundItem)
let firstItem = results.items[0];
} else {
}
} )
.catch( (err) => {
let errorMsg = err;
} );
“title” will be the ID of your first column!
To complete this code you can put it into an own function…
function myFirstFunction() {
wixData.query("DATABASE-A")
.contains("column-1", "value")
.find()
.then( (results) => {
if(results.items.length > 0) {
let myRESULTS = results
let myResultItems = results.items
let myFirstFoundItem = results.items[0].title
console.log(myRESULTS)
console.log(myResultItems)
console.log(myFirstFoundItem)
let firstItem = results.items[0];
} else {
}
} )
.catch( (err) => {
let errorMsg = err;
} );
}
Your CODE is almost complete… (What you still need is to call this function, for example when page loads…like this…)
This is the onReady-Function of your page…(when page is loaded completely this function triggers an action.
$w.onReady(function () { })
Your End-code would look like this…
$w.onReady(function () { myFirstFunction() }) //start of func.
function myFirstFunction() {
wixData.query("DATABASE-A")
.contains("column-1", "value") //or using .eq("ref","value")
.find()
.then( (results) => {
if(results.items.length > 0) {
let myRESULTS = results
let myResultItems = results.items
let myFirstFoundItem = results.items[0]
let myFirstFoundItemsTitle = results.items[0].title
console.log(myRESULTS)
console.log(myResultItems)
console.log(myFirstFoundItem)
console.log(myFirstFoundItemsTitle)
} else {
}
} )
.catch( (err) => {
let errorMsg = err;
} );
}
Test this code with your own DATABASE and take a carefully look into your CONSOLE. You will find it in the preview-Mode on the very bottom of the page.
Or if you are using the published version of your site you can press F-12 in your google-chrome browser and go to CONSOLE.
There you will also be able to find all results, which you have just created a few minutes ago.
The last step of your coding-adventure will be to connect your RESULT-DATA with your LIGHTBOX, to show results in it.
For this you will need to use the Wix-Storage (session or local command).
How to manage that, you will find here in this example …
https://russian-dima.wixsite.com/meinewebsite/lightbox
More informations about Wix-DATA and Wix-STORAGE you will find in the CORVID-API-REFERENCE.
Good luck and happy coding!