How to apply WixDataOptions and OR with http function Query

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:

  1. 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

  2. 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);
        });
}

Hi Nithin.

For your first question: please pay attention, that the WixDataOptions parameter should be supplied to find function, and not to query function as in your example.

For the second question, it seems that you are performing an or operation between 2 different collections, which is not supported. We will improve our API reference documentation to reflect that.

Regards,
Genry.

Hello Genry,

Thanks a lot for your quick reply. Yes…yesterday I found out the issue with my second question and corrected it myself. And it would be great if both the answers to my questions are entered in the wix code reference as it might help others stuck in the same situation.

BTW I would like to say that now wix code support in the forum is improving compared to for example 1 -2 month back. I have complained lot of times in this forum and during some wix user surveys about the bad condition of this forum before…I hope it will continue to improve in future like this… Thanks…

Hi Nithin.

Thanks for you kind words :slight_smile:

Yes, we will improve our documentation to reflect the question asked here.

Glad to hear the issue was resolved for you :slight_smile:

Regards,
Genry.