I know there are a bunch of posts about this already, I did find code, but it’s not working the way I want it to.
import wixLocation from 'wix-location';
import wixData from 'wix-data';
let Links=["/welcomeatoll","/towncenter","/communitycenter","/tenniscourt"]; // global value
// ...
wixData.query("RoomList")
.find()
.then( (results) => {
results.items.forEach((item) => {
let link = "/"+
Links.push
})
} )
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
} );
export function button367_click(event) {
let link = Links[Math.floor(Math.random()*Links.length)];
wixLocation.to("/welcomeatoll","/towncenter","/communitycenter","/tenniscourt");
}
The code above doesn’t work at all. For it to work in the way I don’t want it to, I have to remove the other links, like this:
wixLocation.to("/welcomeatoll");
}
it only brings me to the page that is linked first after “wixLocation to”, and none of the others despite the database with all the URLs being linked… So how do I make it take me to a random page that I have already stored in my database? Please help.
Someone please help. Need to get this done by tomorrow 
This little example is tested and working…
$w.onReady(function () {
let links = ["https://www.google.com", "https://www.ebay.com", "https://www.amazon.com", ]
$w('#button1').onClick(()=>{
let link = links[Math.floor(Math.random()*links.length)];
console.log("FOUND-RANDOM-LINK = ", link);
});
});
Generate a BUTTON on your page (“button1”) and publish your site, or use the Wix-Editor-Preview-Mode to get the OUTPUTS.
What should do this code-snipet here?
wixLocation.to("/welcomeatoll","/towncenter","/communitycenter","/tenniscourt");
Seems to be wrong!
What you want is maybe…
wixLocation.to(link);
After the random-function has done it’s work and found one random LINK out of your “link-ARRAY”, you define your new found link as…
let link = .......
So the found random link is defined in the following variable—> link
So now you want to open the found and declared link, using wix-location…
wixLocation.to(link);
Got it working, thank you so much! Is there a way to make it find a random URL from a dataset collection, though? The code with so many links is a little bit of a mess.
All the answers you have already in your own code.
Normaly your code would work just fine, if you would fix some code-parts of it.
Take a closer look once again onto my provided little example and first try to understand it.
Inspect all code-lines and try to understand all code properly. Then you will be able to fix your own code.
And yes of course it is possible. Everything is possible to generate by code.
Not too good with coding so I’ll just deal with having it look all messy. It works, all that matters. Thanks.