import wixData from ‘wix-data’;>
let options = { suppressAuth: true }
async function searchDatabase (searchString) {
let response = await wixData.query(“myDatabase”).contains(“field”, searchString).find(options)
if (response.items.length === 0 {
console.log(“no results found”)
//whatever you want to do if no results found
} else {
console.log(“results:”)
console.log(response.items)
//whatever you want to do if results found
}
}
NOTE THIS SHOULD ONLY BE IMPLEMENTED ON THE BACKEND SHOULD YOU CHOOSE TO DO THIS
Such advices will only confuse the questioner.
It is clear that the post-opener is not very familiar with coding, so i would not recommend to work on backand in his case. He should first understand what is backend and what is frontend.
And also the shown code for backend would be wrong in this shown example, since following parts are missing, or were not mentioned or explained…
a) shown function is NOT an EXPORT-FUNCTION
b) it was not mentioned that a new backend-module would be needed to be created for this.
c) also the fact of an import to the front-end, what also not has been mentioned.
So like already mentioned before → in this case it would be much easier for the questioner to work on FRONTEND.
First you should make clear the following points:
a) Which DATABASE (CMS) do you want to query (do you want to work with)?
b) What kind of DATABASE is it? → OWN-ONE? CREATED by an APP ?
c) Which PERMISSIONS AND RESTRICTION are given to the DATABASE ?
Once you have larified such questions, you can start to query your database.
Since you work with code, you will need the WIX-DATA-API, that means your code would start with the import of this API to your page…
import wixData from 'wix-data';
Every of your codes should start with —>
$w.onReady(()=>{......your whole code here.....});
…after you have imported the neccessary API to your page.
Sometimes you have to (you can) declare global variables first before your $w.onReady()-part commes into action.
Your code right now would look like…
import wixData from 'wix-data';>
const myCMS = 'enter here the ID of your DATABASE';
$w.onReady(()=>{
});
…which represents the basic-structure of your code.
Now you can add your FIRST-FUNCTION (–> getting data from your wished database)
We expand our code…
import wixData from 'wix-data';>
const CMS = 'enter here the ID of your DATABASE';
const FIELD = 'enter here the FIELD of your DATABASE';
const VALUE = 'enter here the VALUE you want to search for';
$w.onReady(async()=>{
const myData = await get_data(CMS, FIELD, VALUE ); console.log('My-Data: ', myData);
});
function get_Data(cms, field, value) {
return wixData.query(cms)
.contains(field, value)
.find(options)
.then((res)=>{return res})
.catch((err)=>{return err});
}
Replace marked fields with your own data and test the function.
What do you get inside of your console?
—> let options = { suppressAuth: true }
Not needed if your DATABASE is completely → RESCTRICTION-FREE !!!
Make first sure that your database is accessible for everybody (for testings).