Hi all,
Not sure if this is one for this forum or not but I am pretty desperate so thought I would ask the question!
Basically, I have a search function on my Home Page which enables the user to obtain results based on info in up to 8 fields of a collection. The results are shown in one of 5 pages (depending on which section the user decides to search in) using a repeater on each page and all has been working fantastically well since I got this up and running many months ago. It was working well earlier in the week when I was 'showcasing' what it offers to someone but then yesterday I noticed it was not providing the correct results - in all cases it appears to be simply showing the very first item from the relevant dataset collection.
I'm finding this ultra strange as I have done no maintenance to any of these pages since I got them up and running and have not touched any of the coding I have for them. I've done the following checks to try and ascertain where the problem lies:
1) I ran a test search earlier knowing that there are 10 results for the search I was doing and the result page showed up 10 results but all of them showing the data for the first item in the dataset.
2) Checked the dev console for any errors - none showing!
3) Added a console log to my code on the results page to check what results were being obtained using
console.log(`queryResults is ${JSON.stringify(queryResults)}`);
and found that it was returning all of the correct results but just not displaying them in the repeater, instead choosing to show the first item in the dataset collection over and over again!
4) Because the code seems to be working correctly, I have tried removing the connections between the dataset and the repeater and then re-connecting them. I also deleted the dataset altogether from the page before reinstating it and re-connecting it to the repeater - neither of these changed the outcome!
The only other thing I have in my mind to try on top of the above is to literally rebuild the page in question but having to rebuild pages whenever there is a bug just doesn't seem like an efficient way of website building to me.
I'm no expert on coding, but based on the tests I have conducted, I am pretty sure this is not a code issue but I thought I would post here in the hope that someone may have experienced something similar or can spot what the issue may be. It's a really important feature of my site and one that is a bit unique in the market I am in and so without it, it may be costing me visitors :(
If anyone can offer any assistance as to why this is happening it would be appreciated :)
Hey, @mattja19 I was able to look at your site and followed the steps you have posted on this thread, but couldn't recreate the issue you described. Can you confirm if you are still experiencing this issue and if you have submitted a ticket to Wix support can you provide the ticket number for our team to further review?
Hey @mattja19 🖐
After inspecting your site, I have a few comments/suggestions.
I also recommend you to read this article.
A) Reduce the code footprint
Instead of storing each value in the storage individually like this:
local.setItem('word1', value1); local.setItem('word2', value3); local.setItem('word3', value3);
You should store them like this:
const item = { word1: value1, word2: value2, word3: value3 } // Convert the object to string; const stringObject = JSON.stringify(item); local.setItem('searchValues', stringObject);
And get the values like this:
const cache = local.getItem('searchValues'); // Parse the string into an object if (typeof cache === 'string') { const item = JSON.parse(cache); // Then acces the values in the item; const firstWord = item.word1; const secondWord = = item.word2; }
B) Combining similar methods into one call
Instead of performing promises for each element individually, you can do it in one line of code.
// Instead of .. $w('#scrappedStatus').show(); $w('#finalOperatorHeader').show(); $w('#text484').hide(); $w('#operatorText').hide(); // You can do it like this $w('#scrappedStatus, #finalOperatorHeader').show(); $w('#text484, #operatorText').hide();
By doing so, you just shrank 21 lines of code into just 2 lines.
C) Dynamic Events Handlers
Beware of using multiple event handlers for the same component as the newest one replaces the old one.
$w('#btn').onClick(() => { console.log(A); $w('#btn').onClick(() => console.log(B)); })
Clicking on the above button will print "A" to the console in the first time, but if you clicked the button again, it'll print "B", not "A", that's because the function on the onClick event was replaced.
In your code, you're defining an onItemReady() function for the repeater and bind the data of each item to their intended elements, but just before assigning the data from your query to the repeater's data property, you replaced it with another function that will show a button, therefore, when the data is assigned to the repeater, the default values that you set in the editor when designing the repeater were used instead of the actual values, that's because the code that binds the data to these elements got replaced.
D) Query Promises
The function that you use to fetch the data can be written better, you can use one query instead.
function fetchData() { return query.limit(1000).find().then(async x => { let results = x.items; if (x.hasNext()) { const temp = await x.next(); results = results.concat(temp); } return results; }) }
Also, looking at your site, you have 1912 items in your database, and you're running two queries, the first one is returning 12 wrong items out of 1912, and the second one is returning the correct 10 items but what's going on here?
E) The onReady functions
The $w.onReady() function must be used only once per page, you should not use multiple functions of it, read my article to learn more.
Sorry, I couldn't inspect your site further as it's time-consuming, it's been nearly 3 hours, if you need professional help, feel free to reach out.
Hope this helps~!
Ahmad
Hi there,
In order to help, we need to see your code first, a link to the page(s) you mentioned will be much better.