Has anyone else received this error when trying to use Backend to receive iCal for a Booking website?
TypeError: (0 , t.syncBatchIcal) is not a function
Has anyone else received this error when trying to use Backend to receive iCal for a Booking website?
TypeError: (0 , t.syncBatchIcal) is not a function
It’s difficult to say what’s happening and what the error might be without seeing the code you’re using.
Are you able to share the code?
This is the Backend code -
import wixData from 'wix-data';
import { fetch } from 'wix-fetch';
const BATCH_SIZE = 10;
// Helper: Parse iCal Events
function parseIcalEvents(icalText) {
const events = [];
const eventBlocks = icalText.split('BEGIN:VEVENT').slice(1);
eventBlocks.forEach(block => {
const uidMatch = block.match(/UID:(.+)/);
const dtStartMatch = block.match(/DTSTART(?:;VALUE=DATE)?:(.+)/);
const dtEndMatch = block.match(/DTEND(?:;VALUE=DATE)?:(.+)/);
if (uidMatch && dtStartMatch && dtEndMatch) {
events.push({
uid: uidMatch[1].trim(),
start: dtStartMatch[1].trim(),
end: dtEndMatch[1].trim()
});
}
});
return events;
}
// Helper: Convert iCal date to JS Date (date only)
function parseIcalDate(str) {
if (str.length === 8) {
return new Date(`${str.slice(0,4)}-${str.slice(4,6)}-${str.slice(6,8)}`);
} else {
return new Date(str.replace(/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/, "$1-$2-$3T$4:$5:$6Z"));
}
}
function toDateOnly(date) {
return new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
}
// Main backend function: batchIndex = 0 for first 10, 1 for next 10, etc.
export async function syncBatchIcal(batchIndex = 0) {
let synced = 0;
let villasResult = await wixData.query("Villas")
.ne("icalUrl", "")
.ascending("_createdDate")
.limit(1000)
.find();
let villas = villasResult.items;
let batchVillas = villas.slice(batchIndex * BATCH_SIZE, (batchIndex + 1) * BATCH_SIZE);
for (let villa of batchVillas) {
if (villa.icalUrl) {
try {
const response = await fetch(villa.icalUrl);
const icalText = await response.text();
const events = parseIcalEvents(icalText);
for (let event of events) {
const uid = event.uid;
const checkIn = toDateOnly(parseIcalDate(event.start));
const checkOut = toDateOnly(parseIcalDate(event.end));
// Upsert by villa, check-in, check-out
const existing = await wixData.query("Bookings")
.eq("villasReference", villa._id)
.eq("checkInDate", checkIn)
.eq("checkOutDate", checkOut)
.find();
if (existing.items.length > 0) {
await wixData.update("Bookings", {
_id: existing.items[0]._id,
villasReference: villa._id,
uid: uid,
checkInDate: checkIn,
checkOutDate: checkOut,
source: "iCal Import"
});
} else {
await wixData.insert("Bookings", {
villasReference: villa._id,
uid: uid,
checkInDate: checkIn,
checkOutDate: checkOut,
source: "iCal Import"
});
}
synced++;
}
} catch (err) {
console.error(`Error syncing iCal for villa ${villa.villaName}:`, err);
}
}
}
return synced;
}
export const public = {
syncBatchIcal
};
Apart from a slight error in the code which has now been corrected. it is because I was trying to use the web.js file as recommended to use by WIX Studio. There is a bug with that code and I have now found it works using .jsw file.
Not sure how many other people are experience problems with web.js
The AI Bot is programmed to say there is a bug with the new file type.
Awesome! Glad to hear you resolved the bug.
In regards to .jsw and .web.js. .jsw is deprecated and I’d highly recommend moving to .web.js.
I imagine the reason it wasn’t working is the way you are exporting the method to be used in the frontend. .web.js uses webMethod so will need a small change in syntax.
Your:
export async function syncBatchIcal(batchIndex = 0) {
// other code
}
would become:
export const syncBatchIcal = webMethod(Permissions.Anyone, (batchIndex = 0) => {
// other code
});
Docs for it are here - Web Method | Velo
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.