I’m having trouble with
The export async function changeOldClassifiedsToDraft() is getting a parsing error.
Hopefully a simple solution. Not a Java expert on this and would appreciate the help
Working in
Dev mode
Site link
If this is happening on a site, include a live or test site link
What I’m trying to do
Set up a button to execute the code.Query the records in the Classifieds collection with a creation date older than 5 days and set the status to draft to retain records but not publish them.
What I’ve tried so far
Tried automations with no success.
Set up a button to execute the AI code that Velo generated.
Extra context
I highlighted the failing code with a // comment.
I prefer to have the code run weekly at 23:59 on Wednesday, but I’ll use the button for now.
import wixData from "wix-data";
$w.onReady(function () {
const COLLECTION_NAME = "Classifieds";
const STATUS_FIELD = "status";
const TARGET_STATUS = "draft";
const AGE_IN_DAYS = 5;
const PAGE_SIZE = 1000;
$w.onReady(function () {
$w("#btnCleanUpClassifieds").onClick(async () => {
const button = $w("#btnCleanUpClassifieds");
button.disable();
try {
const updatedCount = await changeOldClassifiedsToDraft();
console.log(`${updatedCount} classifieds record(s) changed to draft.`);
} catch (error) {
console.error("Unable to change old classifieds to draft:", error);
} finally {
button.enable();
}
});
});
// **********RIGHT HERE***********
export async function changeOldClassifiedsToDraft() {
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - AGE_IN_DAYS);
let result = await wixData
.query(COLLECTION_NAME)
.lt("_createdDate", cutoffDate)
.ne(STATUS_FIELD, TARGET_STATUS)
.limit(PAGE_SIZE)
.find();
let updatedCount = 0;
while (true) {
for (const item of result.items) {
item[STATUS_FIELD] = TARGET_STATUS;
await wixData.update(COLLECTION_NAME, item);
updatedCount += 1;
}
if (!result.hasNext()) {
break;
}
result = await result.next();
}
return updatedCount;
}
})
