Hello,
So I’ve been trying to make a searchbar that searches through multiple databases, and I’m so close to getting it to work. I’ve gotten it to work with one database easily, but I cant make it work with multiple.
// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import wixwindow from 'wix-window';
import wixlocation from 'wix-location';
import {session} from 'wix-storage';
import wixdata from 'wix-data';
import wixbookings from 'wix-bookings';
import {memory} from'wix-storage'
$w.onReady(function () {
var platform = wixwindow.formFactor;
if (platform === 'Mobile') {
$w('#text16').expand()
} else if (platform === 'Tablet') {
} else if (platform === 'Desktop') {
$w('#text16').collapse()
}
session.removeItem('noresults')
session.removeItem('searchResults')
});
export function queryPhones (search) {
wixdata.query('Devices-Phones')
.contains('title', search)
.find()
.then( (results) => {
return results.items;
})
}
export function queryTablets (search) {
wixdata.query('Devices-Tablets')
.contains('title', search)
.find()
.then( (results) => {
return results.items;
})
}
export function queryLaptops (search) {
wixdata.query('Devices-Laptops')
.contains('title', search)
.find()
.then( (results) => {
return results.items;
})
}
export function queryiPhones (search) {
wixdata.query('Phones-iPhones')
.contains('title', search)
.find()
.then( (results) => {
return results.items;
})
}
export function searchquery () {
session.removeItem('searchResults')
var search = $w('#input1').value;
console.info(search)
if (search === null) {
var defColor = $w('#input1').style.borderColor
var defColor2 = $w('#box4').style.backgroundColor
$w('#input1').style.borderColor = 'rgb(200,0,0)'
$w('#box4').style.backgroundColor = 'rgb(200,0,0)'
setTimeout( function() {
$w('#input1').style.borderColor = defColor
$w('#box4').style.backgroundColor = defColor2
}, 2000)
} else {
var phonesearchResults = queryPhones(search)
var searchResults = phonesearchResults.concat()
if (searchResults.length === 0) {
wixlocation.to('/noresults')
session.setItem('noresults', 'yes')
} else {
var searchString = JSON.stringify(searchResults)
session.setItem('searcchResults', searchString)
wixlocation.to('/search-results')
}
}
}
export function input1_keyPress(event) {
if (event.key === 'Enter') {
searchquery()
}
}
export function input1_mouseIn(event) {
$w('#box4').style.backgroundColor = '#394A7B'
}
export function input1_mouseOut(event) {
$w('#box4').style.backgroundColor = '#566FB8'
}
export function input1_focus(event) {
$w('#box4').style.backgroundColor = '#1E3685'
}
export function input1_blur(event) {
$w('#box4').style.backgroundColor = '#566FB8'
}
export function vectorImage3_click_1(event) {
searchquery()
}
The problem I’m having is that my query functions are not returning arrays, so I cant combine them into a single array using the concat command. I didn’t finish the concat command because it would’t have worked.
It’s important to note that the searchbar and my results are on different pages. I’m displaying my results using a repeater, which worked with one database.
Here is the code for the search results page.
// For full API documentation, including code examples, visit https://wix.to/94BuAAs
import wixdata from 'wix-data'
import {session} from 'wix-storage'
import wixlocation from 'wix-location'
$w.onReady(function () {
var search = session.getItem('searchResults')
var searchResults = JSON.parse()
console.dir(searchResults)
$w('#repeater1').data = searchResults
$w('#repeater1').forEachItem( ($item, itemmdata, index) => {
$item.id = searchResults._id
$item('#text13').text = itemmdata.title;
$item('#text11').text = itemmdata.description;
$item('#text15').text = itemmdata.deviceForm;
$item('#image2').src = itemmdata.repairability;
$item('#gallery1').src = itemmdata.image;
})
});
export function button2_click(event) {
session.removeItem('title')
let $item = $w.at(event.context)
let device = $item("#text15").text
session.setItem('title',device)
wixlocation.to('/repairform');
}
I hope someone can help me with this, thank you.