I have spent 18 hrs a day for the past week trying to figure this out to no avail, someone please help! I am rather new to coding and despite hours of reading I cant grasp loops.
I am trying to make a custom booking platform. I have a 'Timeslot' database with a field for each rental site, '100', '101', '102'.... and a date column with a cell for every day. Another db holds all of a guest reservation details, Check In, Check Out, Site Number..... When a guest completes booking I filter 'Timeslot' db to get an array of all of the items with dates between check in and out. I need to insert the guests name on the field for the site# they're renting on each of those dates.
I have tried several different methods and I believe that because of the structure of my database I will have to update the fields via dataset. I tried other methods like the getItem, update method but they either didnt recognize my numeral database field names or the variables I am using for field name and value.
function updateDB() { const guestSession = $w('#dataset2Guest').getCurrentItem(); const email = $w('#iEmail').value; const checkIn = guestSession.checkIn; const endDate = guestSession.checkOut; const siteNum = guestSession.siteNumber; let yearValue = checkIn.getFullYear(); let monthValue = checkIn.getMonth(); let dayValue = checkIn.getDate(); let startDateSrch = new Date(yearValue, monthValue, dayValue, 0, 0, 0); console.log(startDateSrch) yearValue = endDate.getFullYear(); monthValue = endDate.getMonth(); dayValue = endDate.getDate(); let endDateSrch = new Date(yearValue, monthValue, dayValue, 23, 59, 59); $w('#dataset10').setFilter( wixData.filter() .between("date", startDateSrch, endDateSrch) ) // Process promise result .then(() => { let count = $w("#dataset10").getTotalCount(); console.log("Size of the Dataset: ", count); for(var i = 0; i < count; i++){ $w("#dataset10").getItems(i,1) .then( (result) => { let items = result.items; let date = items[0].date; let totalCount = result.totalCount; $w("#dataset10").setFieldValue(siteNum, $w('#iEmail').value); $w("#dataset10").save().then(() =>{ }) console.log(date); } ) // End getItems .catch( (err) => { let errMsg = err.message; let errCode = err.code; } ); //End Catch Error } // End For Loop }); // End onReady() } // End Function */
I got it! One of the problems was that I was trying to use getItem which was cycling through the filtered items but it didn't set them as the current item. Although I got it to work my code is a mess so I'd still appreciate any input to clean it up. I barely understand the concept of the various bits of code I mashed up until I got the result I wanted.
async function updateDB() {
const guestSession = $w('#dataset2Guest').getCurrentItem();
const email = $w('#iEmail').value;
//-------------------
const checkIn = guestSession.checkIn;
const endDate = guestSession.checkOut;
const siteNum = guestSession.siteNumber;
//-------------------
let yearValue = checkIn.getFullYear();
let monthValue = checkIn.getMonth();
let dayValue = checkIn.getDate();
let startDateSrch = new Date(yearValue, monthValue, dayValue, 0, 0, 0);
//-------------------
console.log(startDateSrch)
yearValue = endDate.getFullYear();
monthValue = endDate.getMonth();
dayValue = endDate.getDate();
let endDateSrch = new Date(yearValue, monthValue, dayValue, 23, 59, 59);
//-------------------
let res = await $w('#dataset10').setFilter(wixData.filter()
.between("date", startDateSrch, endDateSrch)
)
let count = $w("#dataset10").getTotalCount();
console.log("Count: ", count);
$w('#dataset10').getItems(0, count)
console.log('Start')
for (var i = 0; i < count; i++) {
try {
let setCurrent = await $w('#dataset10').setCurrentItemIndex(i)
} catch (error) {
console.log('problem with set') // handle the error here
}
try {
let currentItem = await $w("#dataset10").getCurrentItem()
console.log(currentItem)
let currentItemIndex = $w("#dataset10").getCurrentItemIndex()
console.log(currentItemIndex)
} catch (error) {
console.log('problem with get item') // handle the error here
}
try {
let newValue = await $w("#dataset10").setFieldValue(siteNum, $w('#iEmail').value);
console.log(newValue)
let saveResult = await $w("#dataset10").save();
console.log(saveResult)
} catch (error) {
console.log('problem with save') // handle the error here
}
} // close for loop
}
Thank you so much but it's still not working. Only the first item is updated and saved. When I log it, it basically just reiterates that. The log shows that it loops through each of the items, it logs the item and the corresponding date. However only the first items field value is set and the Data saved log triggered.
I suspect it's something to do with the save function not being completed before the loop cycles thru to the next item. I just cant figure out how to resolve it if that is the issue. The error message save not allowed during save comes up with the second item.
Hello,
i am not the one who likes to work with DATASETs, but try this one...
$w.onReady(async function (){console.log("Page ready...."); $w('#dataset2Guest').onReady(()=> {console.log("The dataset is ready...."); $w("#dataset10").onReady(()=> {console.log("The dataset is ready...."); function updateDB() { const guestSession = $w('#dataset2Guest').getCurrentItem(); const email = $w('#iEmail').value; //------------------- const checkIn = guestSession.checkIn; const endDate = guestSession.checkOut; const siteNum = guestSession.siteNumber; //------------------- let yearValue = checkIn.getFullYear(); let monthValue = checkIn.getMonth(); let dayValue = checkIn.getDate(); let startDateSrch = new Date(yearValue, monthValue, dayValue, 0, 0, 0); //------------------- console.log(startDateSrch) yearValue = endDate.getFullYear(); monthValue = endDate.getMonth(); dayValue = endDate.getDate(); let endDateSrch = new Date(yearValue, monthValue, dayValue, 23, 59, 59); //------------------- $w('#dataset10').setFilter( wixData.filter() .between("date", startDateSrch, endDateSrch) ).then((res) => {console.log("RESULTS: ", res); let count = $w("#dataset10").getTotalCount(); console.log("Count: ", count); for(var i = 0; i < count; i++){ $w("#dataset10").getItems(i,1) .then((result) => { let items = result.items; console.log("Items: ", items); let date = items[0].date; console.log("Date: ", date); let totalCount = result.totalCount; console.log("Total-Item-Count: ", totalCount); //---------------------------------- $w("#dataset10").setFieldValue(siteNum, $w('#iEmail').value); $w("#dataset10").save().then(() =>{console.log("Data saved!");}); }) .catch( (err) => {let errMsg = err.message; let errCode = err.code;}); } }); } }); }); });
Also, check the CONSOLE for RESULTS.