How to use wixLocation.to when the URL is fetched in a variable?

I have a collection which contains the product page URL against each product. Now I am using the following code:

wixData .query( “ProductsModified” )
.eq( “collections” , keyvalue)
.ascending( “sortingValue” )
.find()
.then( (results) => {
if (results. items. length > 0 ) {
itemresult = results. items[ 0 ];
$w( ‘#text102’ ). text = itemresult. name;
$w( ‘#text102’ ). onClick((event) => {
wixLocation. to(itemresult. productPageUrl)});
$w( ‘#text110’ ). text = itemresult. formattedPrice;
$w( ‘#text111’ ). text = itemresult. formattedDiscountedPrice;
$w( ‘#image71’ ). src = itemresult. mainMedia;

However, the wixLocation. to statement is not working.
itemresult.productPageUrl returns the correct URL.

Can anyone help?

Hello @debscool1011 ,

Your code seems alright, are you testing this on your live site? When using preview mode if the URL is a external link that isn’t a reference to your website then it won’t work as you’ll be leaving the editor to a different website. But this would work in live.

Otherwise I would suggest consoling the itemresult.productPageUrl inside the onClick function to make sure that’s working properly. If it is then make sure the URL is a String