Hi all,
I have a database called "songs" where one of the columns is named "youtubeUrl". I am trying to use a function to randomly select a URL from the database and play it in the Youtube video player on my site each refresh.
I tried different ideas I found online but most were related to random image generation, and I couldn't get it working for my case, here is one of the codes I tried attached.
Thanks for your help!
// For full API documentation, including code examples, visit http://wix.to/94BuAAs
import wixWindow from 'wix-window';
import wixData from 'wix-data';
$w.onReady(async function () {
// Check the render env so this is just done once.
// Otherwise, server-side will render, and then the image
// switches to the image that the client-side rendered.
if (wixWindow.rendering.env === 'browser' || wixWindow.viewMode === 'Preview') {
let res = await wixData.query("songs").find();
let items = res.items;
let count = items.length; // how many images do we have?
let rnd = Math.floor(Math.random() * count);
$w('#image1').src = items[rnd].image; // get a random image
$w('#image1').show(); // image on page is hidden, we we now show it
}
});
Hi Kevin 🖐
You already have the code to do it, just get the link of the video instead of the image src.
wixData.query("songs").find().then((result) => { if (result.length > 0) { let count; while (typeof count !== 'number' || !count > 0) { count = Math.floor(Math.random() * result.items.length) } let item = result.items[count]; if (item.video) { $w('#video').src = item.video; } } })
Hope this helps~!
Ahmad