Image Database Field as Image source

I’m trying grab a random record from a collection and display the image (from a field in the record) in an Image control. The random bit seems to be working ok.


I see the path to the image but non of my other fields.

export function button1_click(event) {
wixData.query(“Images”)
.find()
.then( (results) => {
let randomNumber = Math.floor((Math.random() * results.items.length));
console.log(randomNumber)
let randomItem = results.items[randomNumber];
console.log(randomItem)
$w(“#image1”).src = randomItem.Image;
console.log(“ok”)
}); //Add your code for this event here:
}

Any ideas ?

You need to be sure that you are using Field Key (and not Field Name):

You don’t want this:
$w(" #image1 ").src = randomItem. Image ;

You do want this:
$w(" #image1 ").src = randomItem. image ;

Notice that the Field Key starts with a lower-case letter.

thanks

1 Like