Question:
When I load my site page, I get a strange error about 70% of the time stating ["WDE0025: The RegistrationCartItems collection does not exist. You cannot work with a collection using the Data API before it is created in the Editor."]
I understand what this would mean normally, but I do have a RegistrationCartItems collection and it pulls from it perfectly fine about 30% of the time.
Product:
Wix Editor / Velo
What are you trying to achieve:
Have the page load without errors.
What have you already tried:
My code that seems to be causing the issue is below. The error seems to occur when getCartDetails
calls await getMemberCartItems()
.
import { Permissions, webMethod } from "wix-web-module";
import { currentMember } from 'wix-members-backend'
import wixData from 'wix-data';
import wixPayBackend from 'wix-pay-backend';
const REGISTRATION_FEE = parseFloat(100)
const sleep = ms => new Promise(r => setTimeout(r, ms));
/**
* Retrieves current member details.
* @returns {Promise<Object>} The details of the current member.
*/
const getCurrentMemberDetails = async () => {
return await currentMember.getMember({ fieldsets: ["FULL"] });
}
/**
* Retrieves cart items for a specified member.
* @param {string} memberId - The ID of the member.
* @param {boolean} consistentRead - Specifies if the read should be consistent.
* @returns {Promise<Array>} An array of cart items.
*/
const getMemberCartItems = async (memberId, consistentRead) => {
await sleep(500)
const cartResponse = await wixData.query('RegistrationCartItems')
.eq('contact', memberId)
.find({ suppressAuth: true, consistentRead: consistentRead });
return cartResponse.items;
}
/**
* Retrieves applied coupons for a specified member.
* @param {string} memberId - The ID of the member.
* @param {boolean} consistentRead - Specifies if the read should be consistent.
* @returns {Promise<Array>} An array of applied coupons.
*/
const getAppliedCoupons = async (memberId, consistentRead) => {
const couponsResponse = await wixData.query('RCAplCpnCodes')
.eq('contact', memberId)
.find({ suppressAuth: true, consistentRead: consistentRead });
return couponsResponse.items.length > 0 ? couponsResponse.items : [];
}
/**
* Retrieves details of applied coupons.
* @param {Array} appliedCoupons - An array of applied coupons.
* @param {boolean} consistentRead - Specifies if the read should be consistent.
* @returns {Promise<Array>} An array of applied coupon details.
*/
const getAppliedCouponDetails = async (appliedCoupons, consistentRead) => {
const appliedCouponDetails = [];
for (const appliedCoupon of appliedCoupons) {
try {
const result = await wixData
.query('RegistrationCartCouponCodes')
.eq('_id', appliedCoupon.coupon)
.include('applicableCourses')
.find({ suppressAuth: true, consistentRead: consistentRead });
appliedCouponDetails.push(result.items[0]);
} catch (error) {
console.error(`Error processing applied coupon ${appliedCoupon._id}:`, error);
}
}
return appliedCouponDetails;
}
/**
* Adjusts cart items with coupons, showing both original and adjusted prices,
* and calculates the total amount off from coupons.
* @param {Array} cartItems - An array of cart items.
* @param {Array} appliedCouponDetails - An array of applied coupon details.
* @returns {Object} An object containing adjusted cart items and total amount off from coupons.
*/
const adjustCartItemsWithCoupons = (cartItems, appliedCouponDetails) => {
let totalAmountOffFromCoupons = 0;
const adjustedCartItems = cartItems.map(item => {
let originalPrice = item.price;
let newPrice = originalPrice;
const applicableCouponsToItem = appliedCouponDetails.filter(couponDetails => {
const applicableCourseIds = couponDetails.applicableCourses.map(course => course._id);
return applicableCourseIds.includes(item.course);
});
for (const coupon of applicableCouponsToItem) {
let discountAmount = 0;
if (coupon.percentOff > 0 && coupon.percentOff <= 100) {
discountAmount = originalPrice * (coupon.percentOff / 100);
newPrice -= discountAmount;
} else if (coupon.amountOff > 0) {
discountAmount = (coupon.amountOff > newPrice ? newPrice : coupon.amountOff);
newPrice -= discountAmount;
}
totalAmountOffFromCoupons += discountAmount;
}
return {
...item,
originalPrice: originalPrice,
price: parseFloat(newPrice.toFixed(2))
};
});
return { adjustedCartItems, totalAmountOffFromCoupons };
}
/**
* Retrieves and adjusts current member cart items with coupon applications.
* @returns {Promise<Object>} An object containing adjusted cart items and total amount off from coupons.
*/
const getAdjustedCartItemsForCurrentMember = async (consistentRead = true) => {
const member = await getCurrentMemberDetails();
const memberId = member._id;
await sleep(100)
const cartItems = await getMemberCartItems(memberId, consistentRead);
await sleep(100)
const appliedCoupons = await getAppliedCoupons(memberId, consistentRead);
const appliedCouponDetails = await getAppliedCouponDetails(appliedCoupons, consistentRead);
return adjustCartItemsWithCoupons(cartItems, appliedCouponDetails).adjustedCartItems;
}
export const getCartDetails = webMethod(Permissions.SiteMember, async () => {
return await getAdjustedCartItemsForCurrentMember()
})
Additional information:
The error is only visible on site logs, in my console it only shows the following:
createConsoleProxy.ts:47 Error: Unable to handle the request. Contact the site administrator or view site monitoring logs for more information.
I have tried creating a new CMS table and using its name instead. That solved the issue for about 30 minutes then it started happening again ~70% of the time. Any help is greatly appreciated.