Automatically updating database, based on date

Hi all,

Please help me. This is driving me crazy. I’ve created a backend job to search a database and find all entries that are old than three months. I want it to then update the status of each entry to be Inactive.
What am I doing wrong:

import wixData from ‘wix-data’;

async function Expire() {

// Querying all the items in the collection.
return wixData.query(“DATABASENAME").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.FieldID < expirationDate);
oldListings.forEach(item => {
let toUpdate = {
“status”: “Inactive”,
};
// Updating each of this items from the collection.
wixData.update(“DATABASENAME", toUpdate);
})
})
}

@jeremyallen552 A couple things are catching my eye.

  1. The filtering condition is on a field that, by its name, does not seem like it’s a date field. If it is, fine. If not, using the Wix built-in _createdDate would probably suit your purposes.

  2. The update function does not include the _id field, so it does not know what record to update. What I would do is simply assign the status as “Inactive” in the item object itself and have that be the object that the update function uses.

oldListings.forEach(item => {
    item.status = "Inactive"
    wixData.update(“DATABASENAME", item);
})