How to display a database row if no results are found in search?

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!!

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

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;
})
});
}

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.

Please post the URL of your site. Only authorized Wix personnel can get access to your site in the editor.

https://info206933.wixsite.com/website

Hey Bryan,

:exploding_head: 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-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. :slight_smile: Thank you!

Note: You should post a new question so that others may participate and learn.

You can display a message when you set the filter:

Instead of:

$w('#PODPlatform').setFilter(newFilter); 

You can do something like this:

$w("#PODPlatform").setFilter(newFilter)
.then( () => {
    console.log("Dataset is now filtered");
    let count = $w("#PODPlatform").getTotalCount();
    if(count === 0) {
        // display message that no items found
    }
} )
.catch( (err) => {
    console.log(err);
} );

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