Linking search results to Dynamic Pages

After my previous post, I have been trying to solve the issue of having results which don’t have a dynamic page not being clickable.

Having browsed the web, I have come up with the below:

function linktolocolog () {
$w('#Shunters').onReady(() => {
  wixData.query('Shunters')
    .eq('number', number1)
    .find()
    .then(res => {
 const number1 = res.number1;
    })
    .catch ((err) => {
 let errorMsg = err;
    });
    wixData.query('LocoData')
    .eq('number', number2)
    .find()
    .then(res => {
 const number2 = res.number2;
    })
    .catch ( (err) => {
 let errorMsg = err;
    });

 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.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);
 wixLocation.to('https://www.ukraillog.co.uk/'+collectionID+'/'+dynamicPageID);
 })
}
})
}

It doesn’t work but thought I would post in here to see if there is any kind soul out there who could, at the very least, tell me if I am slowly heading in the right direction with the additions I have made in this code?

Thanks