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
To go with Ahmad's post above, you can view this example here.
https://www.wix.com/corvid/forum/tips-tutorials-examples/example-remove-duplicates-from-connected-dropdown-options-using-distinct-query
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