I did add this query onto another post of mine but it may have got lost so thought I'd start a new one.
I currently have a search result page which is linked to a dataset called 'Shunters'. Using the following code, I have been able to create a link from the search results to the relevant dynamic page with the 'number' field from the dataset being the link - so clicking on the search result for number 90001 would take you to the dynamic page for number 90001:
functionlinktolocolog(){
$w('#Shunters').onReady(()=>{
$w("#container2").onClick((event)=>{
let $item = $w.at(event.context);
let currentItem =$item('#Shunters').getCurrentItem();
let dynamicPageID =`${currentItem.number}`
let collectionID =`${currentItem.title}`
console.log('https://www.mywebsite.co.uk/'+collectionID+'/'+dynamicPageID); wixLocation.to('https://www.mywebsite.co.uk/'+collectionID+'/'+dynamicPageID);});})}
Some of the numbers produced in the search results won't have a dynamic page and so currently when clicking on these items I am redirected to a 404 error page. What I would like to do is, instead of being redirected to that error page, is for those search results which don't have corresponding dynamic page to be unclickable. This would require comparing the 'number' field in one dataset (the one on the search results page) to the 'number' field in the dataset on the Dynamic Page and if the numbers exist in both allowing the search result to be clickable and if they don't exist in both then making the search result unclickable.
I have tried a few approaches and have been pointed towards async/await functions and so tried the below:
async function linktodynamicpage () {
$w('#Shunters').onReady(async () => {
const number1 = await wixData.query('Shunters')
.eq('number', number1)
.find();
const number2 = await wixData.query('DynamicPageCollection')
.eq('number', number2)
.find();
if (number1 === number2){
$w("#container2").onClick((event)=>{
let $item = $w.at(event.context);
let currentItem =$item('#Shunters').getCurrentItem();
let dynamicPageID =`${currentItem.number}`
let collectionID =`${currentItem.title}`
console.log('https://www.mywebsite.co.uk/'+collectionID+'/'+dynamicPageID); wixLocation.to('https://www.mywebsite.co.uk/'+collectionID+'/'+dynamicPageID);});})}
When using this code though, none of the search results become clickable where some should be as the number exists in both of the collections so I am clearly doing something wrong but I am not sure what.
Any help would be appreciated :)
Thanks