Hey Wix Team, Guys,
I have a htttpfunction to delete the contents of a database after some date/time of its creation. I am using a query to initially find the items from two collections to delete and then WixData.Remove to delete the items that I wanted to delete.
But I have two issues currently with my code:
-
WixDataOptions is not working in the query function. Why is it so? Am i doing any thing wrong?
If i change the database permission to “read anyone”, I can delete the contents using the code below. But else the query dont give any output as it dont have the permission -
I can’t do Or operation on query. If i do or operation like below code I am getting this error in from return
{"Error":{"code":"WD_VALIDATION_ERROR"}}
If I remove the or operation, then everything works expect issue 1 .
Pleas help!!!
Btw I am calling my function using below link.
https://qqqqqqqq.wixsite.com/qqqqqqqqqqqqqq/_functions/deleteOldDatabaseData
import {
notFound,
ok,
serverError
} from 'wix-http-functions';
import wixData from 'wix-data';
export async function get_deleteOldDatabaseData() {
let itemsToDelete;
let threeMonthAgoDate = new Date(
new Date().getFullYear(),
new Date().getMonth() - 3, // Add the month here(Change)
new Date().getDate()
);
let returnoptions = {
"headers": {
"Content-Type": "application/json"
}
};
let options = {
"suppressAuth": true,
"suppressHooks": true
};
let q1 = wixData.query("Events", options)
.lt("_createdDate", threeMonthAgoDate);
let q2 = wixData.query("Service_Search", options)
.lt("_createdDate", threeMonthAgoDate);
let full_query = q1.or(q2);
return await full_query.find()
.then((results) => {
if (results.items.length > 0) {
itemsToDelete = results.items;
for (let i in itemsToDelete) {
wixData.remove("Events", itemsToDelete[i]._id, options);
}
returnoptions.body = {
"Status": "Items Deleted = " + results.items.length
};
return ok(returnoptions);
}
returnoptions.body = {
"Status": "No Data To Delete"
};
return notFound(returnoptions);
})
// something went wrong
.catch((error) => {
returnoptions.body = {
"Error": error
};
return serverError(returnoptions);
});
}