Search results on results page not showing corrrectly after subsequent searchs from Home page

Hello! I have to start off by saying I don’t know anything about coding, but I have managed over the last three days to read and piece together some code that is working fairly well with only one issue that I suspect won’t be hard to solve, I’m just not finding the solution. I have a very new appreciation for those of you who write the backend of my sites.

I use Wix Studio. I have a search bar on my home page that displays the search results to a repeater on another page. On the Home page, I enter a keyword in the search bar, that takes me to my results page, the keyword is in the search bar on that page, and the items with that keyword are displayed in the repeater, the item count displayed is correct. If I go back to the Home page and enter another keyword, it takes me to the results page, the search bar on that page has the correct keyword, but the repeater is showing ALL items in the collection. BUT, the item count display is showing the number of items that should be there based on the keyword. If I hit enter in the search bar, the correct items will appear. I can also enter a different key word in the search bar on the results page and it will display correctly.

My code for both pages is below. I apologize if I’m not displaying it correctly. Thank you for any help and insight!

This is the code on my Home page:

import wixData from ‘wix-data’;
import { local} from ‘wix-storage’;
import wixLocation from ‘wix-location’;
export function searchInput_keyPress(event) {
if (event.key===“Enter”){
let word=$w(‘#searchInput’).value;
local.setItem(“searchWord”, word);
wixLocation.to(‘/blank-4’)

}}

This is the code on my results page:

import { local } from ‘wix-storage’;
import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

$w.onReady(function () {
var sameWord = local.getItem(“searchWord”);
$w(“#searchInput”).value = sameWord;

wixData.query('AllFacebookGraphics')
    .contains('title', $w("#searchInput").value)
    .find()
    .then(results => {
        $w('#repeater1').data = results.items;

        let number = (results.items.length)

        if (results.items.length > 1)
            $w('#results').text = (number + ' Graphics Found');

        if (results.items.length == 1)
            $w('#results').text = ('1 Graphic Found.');
        if (results.items.length == 0)
            $w('#results').text = ('No Graphics Found.');

        $w("#repeater1").onItemReady(($item, itemData, index) => {
            $item("#sizes").text = itemData.sizes;
            $item("#mainImage").src = itemData.mainImage;
            $item("#mainImage").link = itemData['link-all-graphics-1-title'];

        })
    })
$w("#searchInput").onKeyPress((event) => {
    if (event.key === "Enter") {

        wixData.query('AllFacebookGraphics')
            .contains('title', $w("#searchInput").value)
            .find()
            .then(results => {

                $w('#repeater1').data = results.items;

                let number = (results.items.length)

                if (results.items.length > 1)
                    $w('#results').text = (number + ' Graphics Found');
                if (results.items.length == 1)
                    $w('#results').text = ('1 Graphic Found.');
                if (results.items.length == 0)
                    $w('#results').text = ('No Graphics Found. Try another keyword.')

                $w("#repeater1").onItemReady(($item, itemData, index) => {
                    $item("#sizes").text = itemData.sizes;
                    $item("#mainImage").src = itemData.mainImage;
                    $item("#mainImage").link = itemData['link-all-graphics-1-title'];
                })
            })
    }
})

})

Ah, this looks like a hybrid from my tutorial! Well done! We need more female coders in the Wix world.

I am surprised the search gets triggered on the first try after being redirected from your home page.

You are missing the line of code that waits for the repeater dataset to be ready before you trigger the query to happen. So the code you currently have is a little buggy.

I have attached the original code tutorial above so you can go through it to adjust your code accordingly.

If you prefer to watch the video to follow along, here is the link: https://www.youtube.com/watch?v=ZiKcx2nlrkw

Happy coding!
#codequeen

YES!! I refered to your tutorial often, but something just wasn’t working, so I tried different things. However, I just went back and entered everything exactly as you have it, and it WORKS PERFECTLY! Thank you so much!

1 Like

I’m having another issue, but not sure it’s related to the code. On my laptop, the search bar on the home page and the results page work beautifully. BUT when I use my Android phone, the search bar on the home page does not work. I enter a search term and hit enter, the cursor jumps to the first input field of a form that I have lower on that page. The search bar on the results page works great. Thoughts?

My next endeavor is to search a keyword and filter results based on the content of other fields. I’ve found multiple tutorials on that, but haven’t attempted any coding. I’ll likely be back here asking question in the coming weeks lol. Thanks again!