Help needed creating a loop to check for db items

Hi Guys

I’ve written the code below that finds dates in the db and then sets them as disabled dates in the datePicker on the client-facing side. It works great when I have 10 dates in the db, however the script doesn’t lend itsfelf to having more than 10 items or less than 10 items.

Obviously if I create a loop to check the db for ‘while …hasNext()’ it would solve this problem, but the thing is, after doing quite a bit of reading up on loops, I’m still lost as to how to create a loop for the code below…

$w.onReady(function () {
	$w("#datesDataset").onReady(() => {
		wixData.query("DisabledDates")
			.isEmpty('title')
			.find()
			.then((date1) => {
                                let firstItem = (date1.items[0]);
				let badDate1 = new Date(firstItem.date1.toISOString().slice(0,10));
				let secondItem = (date1.items[1]);
				let badDate2 = new Date(secondItem.date1.toISOString().slice(0,10));
				let thirdItem = (date1.items[2]);
				let badDate3 = new Date(thirdItem.date1.toISOString().slice(0,10));
				let fourthItem = (date1.items[3]);
				let badDate4 = new Date(fourthItem.date1.toISOString().slice(0,10));
				let fifthItem = (date1.items[4]);
				let badDate5 = new Date(fifthItem.date1.toISOString().slice(0,10));
				let sixthItem = (date1.items[5]);
				let badDate6 = new Date(sixthItem.date1.toISOString().slice(0,10));
				let seventhItem = (date1.items[6]);
				let badDate7 = new Date(seventhItem.date1.toISOString().slice(0,10));
				let eigthItem = (date1.items[7]);
				let badDate8 = new Date(eigthItem.date1.toISOString().slice(0,10));
				let ninthItem = (date1.items[8]);
				let badDate9 = new Date(ninthItem.date1.toISOString().slice(0,10));
				let tenthItem = (date1.items[9]);
				let badDate10 = new Date(tenthItem.date1.toISOString().slice(0,10));
			
				$w("#datePicker1").disabledDates = [badDate1, badDate2, badDate3, badDate4, badDate5, badDate6, badDate7, badDate8, badDate9, badDate10];
			
		});
	});
});
1 Like

Hi Tiaan,

const disabledDates = [];
for (let i = 0; i < date1.items.length; i++) {
   disabledDates.push(new Date(date1.items[i].date1.toISOString().slice(0,10)));
}
$w("#datePicker1").disabledDates = disabledDates;

You can also use Map method;
Roi

1 Like

Hi Roy

Thank you!! You made that look too easy! I appreciate the help!

Tiaan