Issue with trying to get data from a dataset

I have a database with a table called events and I have a repeater that shows the events. There is another table called photographs that links to the events table. One event to many photographs. I am trying to populate my Event Item page with data coming from the Photographs table. The idea is to hide text objects if there are no photographs but if photographs are linked to an event and the event is a competition then the text objects will be shown and the text values will be populated with the names of the winners of the competition. There should be only 2 winning photographs one from the advanced and the other from the standard section. The top scoring photos will be displayed in a gallery that will be linked to the text objects. Regrettably I cannot get this to work I have tried lots of different ways. Originally I tried to create a function that ran a data query, the idea was to use the results of the query but I had no success with that even when I made the methods asynchronous. I then decided to add a dataset to the page to hold the photographs that I would filter based on the event and a Boolean flag called winner. I had 2 methods that would be called inside the onReady() of the events and nested photographs datasets. I have successfully returned the correct photograph from my data but I cannot get the information from the photograph instead it reads the photograph as undefined. This is the code I am using -

var eventItem;
var havePhotos; // used to set up form text fields
var filtercount;
$w.onReady(function () {
 // Write your JavaScript here
 // the purpose of the photographsDS is to acertain if there are any photos linked to the event
 // photographsDS is limited to 2 entries ideally for competition where an advanced and Standard winner is chosen
 
    $w('#eventDS').onReady(async function() { // Check that the event has loaded
        eventItem = $w('#eventDS').getCurrentItem();
        console.log("EventId OnReady() ", eventItem._id)
        $w("#photographsDS").onReady(async function () { // check photos have loaded
            findPhotos(); // find photos for our event
 
        });
    });
 // Click "Preview" to run your code
});

async function  findPhotos () 
{
    console.log("Entered findPhotos()")
    havePhotos = false;
 let eventId = eventItem._id;
 if(String(eventId).length > 0) // ensure a valid event _id is passed
    {
        console.log('Event type = ' + eventItem.type)
 let filter = wixData.filter().eq('event',eventId)
 if(eventItem.type === 'C') // check if the event is a competition
        {
            filter = wixData.filter().eq('event',eventId).eq('winner', true);
        }
 try 
        {
 await $w('#photographsDS').setFilter(filter).then((result) => {
                filtercount = $w('#photographsDS').getTotalCount();
                console.log('Page setup filtercount = ', filtercount);
 let photos =  $w('#photographsDS').getItems(1, filtercount);
                console.log('photos =', photos);
 if(filtercount > 0)
                {
                    console.log['result = ',result];
 let msg = 'Congratulations go out to ';
 if(eventItem.type === 'C')
                    {
 // loop the dataset to set the advanced and standard winner text
                        console.log('Filtered Count = ',filtercount)
 for(var i = 0; i < filtercount;i++)
                        {
 let photo = photos.items[i];
 if(photo.type === 'A')
                            {
                                msg += photo.Author + ' in the Advanced section for the winning image ' + photo.title + ' with a score of ' + photo.score;
                                $w('#advancedCongratsTxt').text = msg;
                                console.log(msg);
                            }   
 if(photo.type === 'S')
                            {
                                msg += photo.Author + ' in the Standard section for the winning image ' + photo.title + ' with a score of ' + photo.score;
                                $w('#advancedCongratsTxt').text = msg;
                            }
                            console.log(msg);
                        }
                        console.log("Show Fields for competition setupPage()")
                        $w('#SeeAdvancedTxt').show();
                        $w('#SeeAllTxt').show();
                        $w('#SeeStandardTxt').show();
                        $w('#advancedCongratsTxt').show();
                        $w('#standardCongratsTxt').show();          
                    }
 else
                    {
                        console.log("Show Fields for non competition setupPage()")
                        $w('#SeeAdvancedTxt').hide();
                        $w('#SeeAllTxt').text = 'See Photographs of the event'
                        $w('#SeeAllTxt').show();
                        $w('#SeeStandardTxt').hide();
                        $w('#advancedCongratsTxt').hide();
                        $w('#standardCongratsTxt').hide(); 
                    }
                }
 else
                {
                    console.log("Hide fields No Photos setupPage()")
                    $w('#SeeAdvancedTxt').hide();
                    $w('#SeeAllTxt').hide();
                    $w('#SeeStandardTxt').hide();
                    $w('#advancedCongratsTxt').hide();
                    $w('#standardCongratsTxt').hide(); 
                }
            });
        } catch (err) 
        {
            console.log(err.message);
        }
    }
 else
    {
        $w('#photographsDS').setFilter(
            wixData.filter().isEmpty('_id') // this will ensure that there are no photos in the dataset
        );
    }
} 
From the console logs I can see that the probable cause of the error is  let photo = photos.items[i]; because for some reason photos is undefined.


Can someone please look over the code and explain to me what I am doing wrong.

I still have no idea why the code in my original post does not work so I have come up with a better solution. I created a multireference field on the events database which points to photographs.

I then changed my code as shown below

$w.onReady(function () { 
    $w('#eventDS').onReady(async function() { // Check that the event has loaded
        eventItem = $w('#eventDS').getCurrentItem();
        console.log("EventId OnReady() ", eventItem._id)
        findPhotos();
    });
 // Click "Preview" to run your code
});


function findPhotos()
{
 let eventId =  eventItem._id;
    console.log('In test ');
    wixData.query('Events').eq('_id',eventId).include('photographs')
    .find()
    .then((results) => {
        filtercount = results.items[0].photographs.length ;
 let photos = results.items[0].photographs;
        setupPage(photos);
    }); // Get all photographs linked to the event
    console.log('done query')
}

function setupPage(photos) 
{
    console.log("Entered setupPage()")
    $w('#advancedCongratsTxt').text = '';
    $w('#standardCongratsTxt').text = '';
 if(filtercount > 0)
    {
 if(eventItem.type === 'C')
        {
 // loop the dataset to set the advanced and standard winner text
            console.log('Filtered Count = ',filtercount)
 for(var i = 0; i < filtercount; i++)
            { 
 let msg = 'Congratulations go out to ';
 let photo = photos[i];
 if(photo.winner)
                {
 if(photo.type === 'A')
                    {
                        msg += photo.author + ' in the Advanced section for the winning image ' + photo.title + ' with a score of ' + photo.score;
                        $w('#advancedCongratsTxt').text = msg;
                        console.log(msg);
                    }   
 if(photo.type === 'S')
                    {
                        msg += photo.author + ' in the Standard section for the winning image ' + photo.title + ' with a score of ' + photo.score;
                        $w('#advancedCongratsTxt').text = msg;
                        console.log(msg);
                    }
                }
            }
            console.log("Show Fields for competition setupPage()")
            $w('#SeeAdvancedTxt').show();
            $w('#SeeAllTxt').show();
            $w('#SeeStandardTxt').show();
            $w('#advancedCongratsTxt').show();
            $w('#standardCongratsTxt').show();          
        }
 else
        {
            console.log("Show Fields for non competition setupPage()")
            $w('#SeeAdvancedTxt').hide();
            $w('#SeeAllTxt').text = 'See Photographs of the event'
            $w('#SeeAllTxt').show();
            $w('#SeeStandardTxt').hide();
            $w('#advancedCongratsTxt').hide();
            $w('#standardCongratsTxt').hide(); 
        }
 
    }
 else
    {
        console.log("Hide fields No Photos setupPage()")
        $w('#SeeAdvancedTxt').hide();
        $w('#SeeAllTxt').hide();
        $w('#SeeStandardTxt').hide();
        $w('#advancedCongratsTxt').hide();
        $w('#standardCongratsTxt').hide();
    }
 
}

I have posted this as someone may need a similar solution

Well done! You are now a step further. Someone who can solve his own issues, makes faster progress! :wink: