How to Execute Timed Tasks with the Job Scheduler

Watch how to automate your workflow with Corvid by Wix. In this video, we'll show you have to use the Job Scheduler tool to execute timed tasks on a daily, weekly or monthly basis.

Watch Now!

Clearing old listings function:

import wixData from 'wix-data';

export function clearOldListings() {
// Querying all the items in the collection.
return wixData.query("apartmentListings").find().then(results => {
// Declaring a variable for an expiration date to test by initiating it as a date object.
let expirationDate = new Date();
// Setting the expiration date for 3 months prior.
       expirationDate.setMonth(expirationDate.getMonth() - 3);
// Using the forEach method to create a filtered array with listings older than 3 months.
let oldListings = results.items.filter(item => item.uploadedOn < expirationDate);
       oldListings.forEach(item => {
// Removing each of this items from the collection.
           wixData.remove("apartmentListings", item._id);
       })
   })
}

Job Scheduler example code:

{
"jobs": [{
"functionLocation": "/module/filename.funcName",
"description": "some description",
"executionConfig": {
"time": "09:00",
"dateInMonth": 1
           }
       }
   ]