I have a database that has a list of our clients locations. I have a search box set up to do a search of that database by zip codes. If a search is an exact match, it returns the result. This part I've got working thanks to code I've found on the forum. My question is -
Say the search does NOT have a match, how can I make it so that a specific row in the same database is displayed. It's the very first row (#1) and the title of the row is 000.
This is a generic display that will be designed as a sales ad if I can get this part working.
Here is the search code I found on the forum
function search() { wixData.query('ZipCodes') .eq('serviceZip', $w("#searchBar1").value) .find() .then(res => { $w('#repeater1').data = res.items; }); }
Please Help!!
Thank you in advance!!
Hey Bryan,
🤯 Wow! I hate it when I'm stupid.
My use of .catch() was incorrect. Not getting results is not an error, it's just a case of a valid query with no results.
This is what you want:
function search() { Â Â Â Â wixData.query('ZipCodes') Â Â Â Â Â Â Â Â .eq('serviceZip', $w("#searchBar1").value) Â Â Â Â Â Â Â Â .find() Â Â Â Â Â Â Â Â .then(res1 => { if (res1.items.length === 0) { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â wixData.query('ZipCodes') Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â .eq('title', "000") Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â .find() Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â .then(res2 => { Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $w('#repeater1').data = res2.items; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }) Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â .catch((error) => { let errorMsg = error; Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â }); Â Â Â Â Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â Â Â Â Â $w('#repeater1').data = res1.items; Â Â Â Â Â Â Â Â }) Â Â Â Â Â Â Â Â .catch((error) => { let errorMsg = error; Â Â Â Â Â Â Â Â }); }
I hope this helps. I'm now going to go drown my sorrows in beer and cholesterol.
Yisrael
Yisrael, I've tried using your code example above on my site. It hasn't worked and it's stopped the search function from returning results when there are results to show.
I'll post a new post on this in a moment, but would love to get your feedback on where I've gone wrong.
Kind regards,
Leigh
@Yisrael (Wix) Hi! I need your help. How do I display an error message in this code:
import wixData from "wix-data"; $w.onReady(() => { }); let lastFilterplatfromTheyIntegrate; let lastFilterplatformName; let debounceTimer; export function input7_keyPress(event, $w) { if (debounceTimer) { clearTimeout(debounceTimer); debounceTimer = undefined; } debounceTimer = setTimeout(() => { filter($w('#input7').value); }, 50); } function filter(platformName, platfromTheyIntegrate) { { if (lastFilterplatfromTheyIntegrate !== platfromTheyIntegrate || lastFilterplatformName !== platformName) { let newFilter = wixData.filter(); if (platformName) newFilter = newFilter.contains('platformName', platformName); if (platfromTheyIntegrate) newFilter = newFilter.contains('platfromTheyIntegrate', platfromTheyIntegrate); $w('#PODPlatform').setFilter(newFilter); lastFilterplatformName = platformName; lastFilterplatfromTheyIntegrate = platfromTheyIntegrate; } } } Basically, this is a code for a repeater. What I want is to show error message when there's no result. :) Thank you!
https://info206933.wixsite.com/website
Please post the URL of your site. Only authorized Wix personnel can get access to your site in the editor.
Also, just to let you know, I took out this part -
.catch((error) => {
// if we get here, it really is an error!
let errorMsg = err;
My reason for taking it out is that when I went to test this in preview mode it didn't work. My thinking was it had something to do with the error codes it gave once I added the script you provided. The 2 error codes were -
Red Circle - 'err' is not defined
Yellow Triangle - 'error' is already declared in the upper scope.
I took them out just out of curiosity and nothing was changed in how it functioned.
Hi Yisrael,
Thank you for taking the time to assist me with this! I have very little experience working with code so your knowledge and time is most appreciated!!
I have added the snippet of code you provided me with to my site. The search feature still seems to work if I type in a correct zip code. However, if I type in a zip code not in the database it displays a blank result (nothing) and not the generic one I would like to have displayed. I am going to share the whole snippet of code with you to review and confirm I did add it correctly. If I did do that correctly, do you have any other suggestions or ideas to make this work?
Thanks again for your time!
Bryan
function search() { wixData.query('ZipCodes') .eq('serviceZip', $w("#searchBar1").value) .find() .then(res => { $w('#repeater1').data = res.items; }) .catch((error) => { wixData.query('ZipCodes') .eq('title', "000") .find() .then(res => { $w('#repeater1').data = res.items; }) }); }
Add a .catch() for the "error" condition of there not being results for your query. Then do another query for the "000" row. Something like this:
function search() { wixData.query('ZipCodes') .eq('serviceZip', $w("#searchBar1").value) .find() .then(res => { $w('#repeater1').data = res.items; }) .catch((error) => { // normally, catch is for error conditions, however... // you can display the row you want here wixData.query('ZipCodes') .eq('title', "000") .find() .then(res => { $w('#repeater1').data = res.items; }) .catch((error) => { // if we get here, it really is an error! let errorMsg = err; }); }); }
This isn't tested, but it should be OK.
I hope this helps,
Yisrael