Help Sorting By Date In Referenced Table

Hello,

I was hoping someone could help me out with an issue I’m having. I have two collections set up:

events
{eventName, eventId, hostVenue, eventDate}

and

bookingsConfirmed
{eventId, artistId, setTime}

They are connected by reference; eventId from bookingsConfirmed references eventId from events. On my page I have a repeater to display an artist’s upcoming events. I have the following code added on the page:

 //repeater
 $w("#upcomingGigsRep").onItemReady( ($item, itemData, index) => {
     $item("#upcomingEventName").text = itemData.eventId.eventName;
     $item("#upcomingVenueName").text = itemData.eventId.hostVenue;
     $item("#upcomingDay").text = itemData.eventId.eventDate.toLocaleDateString("en-US", {weekday: "long"});
     $item("#upcomingDate").text = itemData.eventId.eventDate.toLocaleDateString("en-US", {day: "numeric", month: "short"});
 }); 

 wixData.query("BookingsConfirmed")
 .include("eventId")
 .eq('artistId', wixUsers.currentUser.id)
 //.ascending("eventId.eventDate") // need something like this
 .find()
 .then( (results) => {
     console.log(results.items);
     // add query results to repeater
     $w("#upcomingGigsRep").data = results.items;
  });

At the moment everything works fine except I can’t figure out how to display the events in order of their date. the query returns the following block:

{ artistId: "d3b94669-914f-4ae8-a132-7aeaa0fa47ac",
  eventId: 
        { eventDate: Sat Jul 06 2019 00:00:00, 
          eventId: "12349",
          eventName: "Josh's Party",
          hostVenue: "Josh's Place",
          title: "12349",
          _createdDate: Fri Jul 05 2019 17:37:08, 
          _id: "75a4f5c9-7634-4402-a784-6ae2de9c700e",
          _owner: "32dc03df-eca0-4d3c-84d7-4480fd08ee40"
          _updatedDate: Fri Jul 05 2019 18:01:13,
          __proto__: Object
        },  
  setTime: "8:30"
}   

I have simplified the above block a bit for easier readability, but the theory is the same. I am trying to sort by eventDate so I would need to access that through eventId, something like

sort().ascending("eventId.eventDate") 

but I can’t figure out the syntax.

Any suggestions?

Thanks.

1 Like

Why not trying to add either

Wix DataSort
https://www.wix.com/corvid/reference/wix-data.WixDataSort.html

Wix setFilter
https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFilter

These previous posts should give you a helping start too.
https://www.wix.com/corvid/forum/community-discussion/sort-a-query-filter
https://www.wix.com/corvid/forum/community-discussion/table-with-multiple-dataset-filtering
https://www.wix.com/corvid/forum/community-discussion/filtering-by-reference-fields

Thanks for the response, I ended up figuring it out thanks to the links you provided. Here’s how it looks now:

$w.onReady(function () {

    $w("#upcomingGigsRep").onItemReady( ($item, itemData, index) => {
        $item("#upcomingEventName").text = itemData.eventName;
        $item("#upcomingVenueName").text = itemData.hostVenue;
        $item("#upcomingDay").text = itemData.eventDate.toLocaleDateString("en-US", {weekday: "long"});
        $item("#upcomingDate").text = itemData.eventDate.toLocaleDateString("en-US", {day: "numeric", month: "short"});
    });

    wixData.query('BookingsConfirmed')
    .include('Events')
    .eq('artistId', wixUsers.currentUser.id)
    .find()
    .then( (results) => {

         var array = []
         for (var key in results.items) {
             if (results.items.hasOwnProperty(key)) {        
                 array.push(results.items[key].eventId);
             }
         }
 
         wixData.query("Events")
         .hasSome('_id', array)
         .ascending("eventDate")
         .find()
         .then( (results1) => {
             $w("#upcomingGigsRep").data = results1.items;
         });
     });
});


Works alright but I have a feeling there is a simpler way to do it.

Anyway, thanks for the help!