Display image from database based on today's date

Hello there,

I have a database with 365 image in it:

and i tried to write a script like this:

import wixData from ‘wix-data’ ;

$w . onReady ( function () {
let localDate = new Date ();
let yearValue = localDate . getFullYear ();
let monthValue = localDate . getMonth ();
let dayValue = localDate . getDate ();
let dateValue = new Date ( yearValue , monthValue , dayValue , 0 , 0 , 0 );

wixData . query ( '365sketch' ) 
. eq ( 'imageDate' ,  dateValue ) 
. find (). then ( results  => { 
    console . log ( results ) 
    $w ( '#dailyimage' ). src  =  results . items [ 0 ]. image ; 
}); 

});

My goal is to display an image of the current day in the middle of the site from the database, but i always get this error:

" Cannot read properties of undefined (reading ‘image’) "

I’m quite new to coding so help would be appreciated. Many thanks!

Hi - first, I’d make sure the dataset is ready - $w(“#365sketch”).onReady( () => {
console.log(“The dataset is ready”); after the page onReady statement and maybe pair the .then with a . catch ( ( err ) => { console.log (err))
Be sure your calculated date/time string is formatted exactly as in your collection.
Hope this helps.

It doesn’t appear that @agiszerint is using a dataset, they are using a wixData query so there is no dataset for an onReady() .

However, you’re probably right about the date possibly not matching. Maybe time zone/locale/whatever.

@agiszerint , You should try using a console.log(dateValue) statement to see what the value of the dateValue variable is. This way you’ll know if it actually contains a value that’s in the database collection.