Hi all,
I’m new to website development and new to corvid coding so forgive me if I don’t explain myself very well!
Basically, I have spent the last month or so researching on various parts of the interweb with regards to setting up a site wide search function on my website which would use one search bar to search all data in all of my database collections. After lots of frustration and stress, my perseverance was rewarded when I finally got the search bar to correctly hunt for data spread across 2 of my database collections by using the following code:
import { local } from 'wix-storage';
import wixData from 'wix-data';
$w.onReady(function () {
var sameWord = local.getItem("searchWord");
$w("#searchBar").value = sameWord;
$w("#searchBar").placeholder = sameWord;
$w('#Class01'),$w('#Class02').onReady(function () {
search();
});
});
export function searchButton_click_1() {
search();
}
function search() {
wixData.query('Class01')
.contains('number', $w('#searchBar').value)
.or(wixData.query('Class01').contains('operator',$w('#searchBar').value))
.or(wixData.query('Class01').contains('livery',$w('#searchBar').value))
.find()
.then(res => {
$w('#repeater1').data = res.items;
wixData.query('Class02')
.contains('number', $w('#searchBar').value)
.or(wixData.query('Class02').contains('operator',$w('#searchBar').value))
.or(wixData.query('Class02').contains('livery',$w('#searchBar').value))
.find()
.then(res => {
$w('#repeater2').data = res.items;
});
}
This code was reached via a mix of great advice and tips found online, some educated guessing (basically I found a code that allowed one database to work in the search and adapted it to fit in another database collection and a 2nd repeater) and then, as most of us newbies need, I had a bit of luck along the way.
The above code is yielding the correct outcome from data I input into my search bar, however just as I was getting excited that things were working, I then noticed that when I input data relating to results in repeater2 into the search bar, it was searching the correct info but each item in that repeater was being duplicated and therefore appearing twice in the results. I conducted a ‘blank’ search in the search bar to bring up all results and it did indeed show that each item in repeater 2 was appearing twice. I checked my database collection and there were no duplicates there and when I check the repeater in ‘preview’ mode no duplicates appear there either. That makes me wonder if it is an issue with the above code that is causing these duplications to appear in the live site after I publish it where they don’t appear in the database collection or the preview page of the repeaters?
I have tried my best to avoid requesting help as I prefer to be a problem solver myself, however I think I have reached the point where I need assistance to be able to continue with my project.
If anyone can point me in the right direction, it would be massively appreciated.
Thanks
Hi
You need to use the new function distinct() , this function make sure to return only the unique items (no duplicates).
wixData.query("myCollection").distinct("number")
Note that you cannot use distinct() and find() at the same time.
Hope that helped~!
Ahmad
Hi Ahmad,
Thanks for taking the time to respond. Having read up on distinct() I am sure that this is the answer to my problem, however I have so far been unable to make it work. My efforts have actually stopped the search from functioning at all so not sure what I am doing but I shall put it down to the fact that it is late here and tiredness has set in. I shall have another crack at it tomorrow but just wanted to thank you in the mean time
Hi again Ahmad,
I have had a play around with distinct() but can’t seem to make it work. Even though I understand the concept of how distinct() will work, none of the many variables of it that I have attempted have yielded the correct outcome. Based on your assistance previously and taking what you put at absolute face value lead me to this:
wixData.query("Class02").distinct("number")
.contains('number', $w('#searchBar').value)
.or(wixData.query('Class02').contains('operator',$w('#searchBar').value))
.or(wixData.query('Class02').contains('livery',$w('#searchBar').value))
.then(res => {
$w("#repeater2").data = res.items;
});
As this failed, and with an assumption that this was maybe too simplistic, I attempted to add distinct() to other parts of the code but to no avail. I’ve read through the thread posted by GOS as well but as that involves dropdowns (same as most examples when searching for help online) I feel it is just adding to my confusion. I mean, should I be adding parts revolving around distinctList and unshift or is that specific information regarding the build of dropdown options?
Equally, I would also love to understand why the repeater is duplicating items in the first place when there is no such duplication within my database collection or the relevant dataset? Seems very strange that it would be do that, particularly when it happens with ‘repeater2’ but not with ‘repeater1’
Hi
I think your problem is that you’re calling the distinct() function before filtering the query.
Wrong way:
wixData.query("Class02")
.distinct("number")
.contains('number', $w('#searchBar').value)
Right way:
wixData.query("Class02")
.contains('number', $w('#searchBar').value)
.distinct("number")
distinct() function is similar to find() function, and must be placed at the end of the query.
Another note for you, you’re populating the repeater with code, so there are many possible reason for the issue.
Thanks Ahmad.
I did the equivalent of switching it off and on - repopulated my database collection and re-linked it to the relevant repeater and this seems to have corrected whatever was causing the duplication in the first place.
Should the issue arise as I produce more repeaters I shall revisit your latest advice. I’ve learnt something already though with regards to where distinct() should be placed so thanks very much for that and for your support on this matter.
Regards
Matt
Hi
I think it was just a conflict between the dataset and the code each of them trying to set things up.
You’re welcome