I have a page where I work with collections, the work is pretty heavy:
Some if-logic, merging, filtering & grouping collections in custom js formula. Result perfectly works in Editor and shows no errors in console except this warning:
When I publish my site everything else is working except this page. I see that collection is loaded (number of items displayed) but items not displayed after processed by my custom formula. And there is Error in console (not sure if its just a coincidence or not):
Iām new to JS, maybe I did some mistake and I cannot recognize it.
Maybe problem is that I set my variables to collection variables in formula
Like can I do this? let x = collection variable etcā¦
I have no idea what can cause this problem
let collectionClientWishlistMy = collections['5aa426a7-4fdc-44c9-a6f6-331875a23883']['data'].map(obj => ({ ...obj, diff: 'wishlist' }));
let collectionClientClothesMy=collections['6a7b27ac-a8bc-4824-919b-897b28040d58']['data'].map(obj => ({ ...obj, diff: 'clothes' }));
let collectionClientWishlistFromStylist=[];
let collectionStylistClothesFromClient=collections['bac5cee2-073d-41ac-8bed-217ae7194be2']['data'].map(obj => ({ ...obj, diff: 'clothes' }));
let collectionStylistWishlistFromClient=[];
let collectionStylistWishlistForClient=[];
/*----------------FILTER BY ROLE---------------------*/
let mergedCollection = [];
if ( variables[/* SELECTED ROLE */'ba37e4b9-a351-4015-9062-d14cd5d100d5'] =="user" )
{
mergedCollection = [...collectionClientWishlistMy, ...collectionClientClothesMy, ...collectionClientWishlistFromStylist];
}
else if (variables[/* SELECTED ROLE */'ba37e4b9-a351-4015-9062-d14cd5d100d5'] =="stylist" )
{
mergedCollection = [...collectionStylistClothesFromClient, ...collectionStylistWishlistFromClient, ...collectionStylistWishlistForClient];
}
/*----------------FILTER BY PAGE---------------------*/
let clothesByPage =[];
if ( variables['59113970-c7fb-4902-9709-48de0151c194'] == "capsule"){
clothesByPage = mergedCollection.filter((x) => x.capsule == true);
}
else if ( variables['59113970-c7fb-4902-9709-48de0151c194'] == "clothes"){
clothesByPage = mergedCollection.filter((x) => x.diff === "clothes");
}
else if ( variables['59113970-c7fb-4902-9709-48de0151c194'] === "wishlist"){
clothesByPage = mergedCollection.filter((x) => x.diff === "wishlist");
}
/*----------------GROUPING---------------------*/
let groupingKey = variables['fbe73684-da7d-4190-a5ba-6cd4fea85e28'];
if ( groupingKey != "season") {
function groupBy(arr, key) {
const groups = {};
arr.forEach(item => {
const groupKey = item[key] || "";
if (!groups[groupKey]) {
groups[groupKey] = { name: groupKey, items: [] };
}
groups[groupKey].items.push(item);
});
return Object.values(groups);
}
return groupBy(clothesByPage, groupingKey);
}
/*----------------SEASON---------------------*/
else if ( groupingKey == "season") {
function groupBy(clothesByPage, groupingKey) {
const result = {};
clothesByPage.forEach((item) => {
const keys = item[groupingKey];
if (keys && keys.length > 0) {
keys.forEach((key) => {
if (!result[key]) {
result[key] = { name: key, items: [] };
}
result[key].items.push(item);
});
} else {
if (!result.empty) {
result.empty = { name: 'ŠŠµŠ· ŃŠµŠ·Š¾Š½Ń', items: [] };
}
result.empty.items.push(item);
}
});
return Object.values(result);
}
return groupBy(clothesByPage, groupingKey);
}