Hi wix community !
my goal is to make a repeater with images which change every day randomly with a cron exression.
I succeed to change the images template by putting the code in public file, but since I can’t call a job scheduler in public and only backend, I’m having issue
Here is my Home code page :
-----------------------HOME---------------------------------------------------
import {makeRepeater} from ‘public/rubriqueConfig.js’;
import {onceADay} from ‘backend/rubriqueBackend.jsw’;
$w.onReady( async function() {
onceADay()
} );
----------------------------rubriqueConfig.js---------------------------------------------
import wixData from ‘wix-data’;
export function makeRepeater(listNumber) {
let dataset = wixData.query(“rubrique”);
dataset.find()
.then( (results) => {
let datasetLength = results.length;
$w(“#repeater”).forEachItem( ($item, itemData, index) => {
$item(“#imageRepeater”).src = results.items[listNumber[index]].image;
})
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
console.log(errorMsg, code);
});
}
----------------------------rubriqueBackend.jsw---------------------------------------------
import {makeRepeater} from ‘public/rubriqueConfig.js’;
let listNumber = ;
const numberOfImage = 6;
// this works fine
function getNumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min +1)) + min;
}
// this works fine
function getListNumber() {
listNumber.push(getNumber(1,23));
for (var i=0; i<5 ; i++) {
let number = getNumber(1,23);
while (listNumber.includes(number)) {
number = getNumber(1,23);
}
listNumber.push(number);
}
}
export function onceADay() {
getListNumber(); // we fill the list with random number
makeRepeater(listNumber);
}
----------------------------------------------jobs.config--------------------------------
// /backend/jobs.config
{
“jobs”: [ // Define up to 20 different jobs
// Choose one of the options below to define the job execution interval:
{
// Option 1 - define execution interval using a cron expression
“functionLocation”: “/rubriqueBackend.jsw”, // Relatively to Backend folder, started by slash
“functionName”: “onceADay”,
“description”: “change rubrique images disposition”, // Optional
“executionConfig”: {
“cronExpression”: “* * * * *” // Set intervals every minutes
// Read about Cron expressions: [cron - Wikipedia]
}
},
{
// Option 2 - define execution interval by setting time, day of week and day of month
“functionLocation”: “/rubriqueBackend.jsw”, // Relatively to Backend folder, started by slash
“functionName”: “onceADay”,
“description”: “describe your job”, // Optional
“executionConfig”: {
“time”: “22:00” // “hh:mm” 24h format, UTC timezone (e.g. 13:00)
// Optional - uncomment section below for more complex intervals
//“dayOfWeek”: “Monday”, // Day of week
//“dateInMonth”: 1 // Number between 1-31
}
}
]
}
I get the $w is not defined null, I look and find from a non official source that $ can be used only in a onReady function ? That cron expression can be called only from backend, I feel there is an issue between my backen and frontend If you have any idea, i’ll be glad !
Thanks