Wix code that will retrieve a random database entry each time a button is clicked

Hi there, I wonder can anyone help? I need to add some code to a Wix webpage so that there is a button and on clicking it will retrieve one profile from a database (called Members). Each time the button is clicked it should choose a different profile to display in a table (with basic details that links to the main profile page). Essentially I would like different people coming to the site to retrieve a different profile from my database by clicking this button. Anyone got any suggestions? Thanks

Have a look at Yisrael’s page and see if you can modify it for your own use.

1 Like

Thanks givemewhiskey. That helped a lot. I now have the page set up but for some reason it keeps pulling the same image. The first in the database. I get this error “Wix code SDK Warning: The src parameter of “image1” that is passed to the src method cannot be set to null or undefined. Line 15” Can you advise on fixing? and also would you know how to add a button that I can link to the full profile of the person who’s image is chosen? Many Thanks.

$w.onReady( async function () {

// Check the render cycle 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.renderCycle === 1) {
let res = await wixData.query(“Members”).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

Am guessing that line 15 is this one:

$w('#image1').src = items[rnd].image;   // get a random image

Plus that you do have this above the onReady function too:

import wixWindow from 'wix-window';
import wixData from 'wix-data';

$w.onReady(async function () {

Am going to say that it is probably just down to the fact that not all your members have images on their profile.

As for the button, you can always setup a button the same as the background image and hidden on load, which when the user clicks on it, then it brings up a lightbox with the members full profile, or whatever you are wanting another member to see of another member’s profile.

Thanks, All the users do have pictures. I have managed to get it working in preview but not when published so have a support ticket with wix as to why that is. Don’t understand how to create the button though. More details would be helpful.