I'm looking for some best practices to help loop some information out of a Wix Events Database.
Basically I have a contact us page, where I want to display the next 4 events in our db when compared to todays date. But I need display the next dates very minimally. (for example)
MON 8:00pm - Reading Group
TUES 5:00pm - Toastmasters
I need the output to be a single string to drop into the textbox on the contact page.
This is how I built the code, which works, but is a bit painful with all the variables..
Is there a better way? (eventually I will add the search regarding the date)
const options = {weekday: "short"};//default the date to just the day of the week wixData.query("Events/Events") .descending("start") // .gt("Start", today) .limit(4) .find() .then( (results) => { if(results.items.length > 0) { let Item1 = results.items[0]; //set item to 1 row of array let Item2 = results.items[1]; //set item to 2 row of array let Item3 = results.items[2]; //set item to 3 row of array let Item4 = results.items[3]; //set item to 4 row of array let date1 = Item1.start; // get the date of row 1 let date2 = Item2.start; // get the date of row 2 let date3 = Item3.start; // get the date of row 3 let date4 = Item4.start; // get the date of row 4 let Event1 = date1.toLocaleDateString('en-US',options)+" "+Item1.scheduleStartTimeFormatted+" - "+Item1.title; let Event2 = date2.toLocaleDateString('en-US',options)+" "+Item2.scheduleStartTimeFormatted+" - "+Item2.title; let Event3 = date3.toLocaleDateString('en-US',options)+" "+Item3.scheduleStartTimeFormatted+" - "+Item3.title; let Event4 = date4.toLocaleDateString('en-US',options)+" "+Item4.scheduleStartTimeFormatted+" - "+Item4.title; $w("#schedule").text = Event1+"\n"+Event2+"\n"+Event3+"\n"+Event4; } else { $w("#schedule").text = errortxt; } } ) .catch( (err) => { let errorMsg = err; } );
What about something like this one....?
let myEvents = []; for (let i = 0; i < results.items.length; i++) { const event = results.items[0].start.toLocaleDateString('en-US',options)+" "+results.items[i].scheduleStartTimeFormatted+" - "+results.items[i].title; console.log("Event: ", event); myEvents.push(event); } console.log("My-Events: ", myEvents);
I mean yes and no, you are essentially doing it. What you could do is use a repeater to maybe display the information, and then just attach the array to the repeater, and code in the itemData on the onItemReady function. I think that might be alot less code for you.