Hey guys,
I would like to show a random picture from my dataset, that stores a growing number of images.
The code I found online returns an error message. Do you know how I can fix the code or if there is an easier way to display a random picture?
My code:
// For full API documentation, including code examples, visit Velo API Reference - Wix.com
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( “UploadedPictures” ).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
}
});
Error message:
Loading the code for the 2 Rate page. To debug this code, open la9wx.js in Developer Tools.
Wix code SDK Warning: The src parameter of “image1” that is passed to the src method cannot be set to null or undefined.
Help is highly appreciated!