Hey guys, wix team,
How can I return multiple values in a get http function call. In the below code, I am deleting items from two databases using a get http function call. But the problem is that I cant get the final result (return value) from the two delete operation. What am I doing wrong.? I have tried all the methods from this link Return multiple values in JavaScript? - Stack Overflow . But nothing is working. Please help. Thanks
import {
notFound,
ok,
serverError
} from 'wix-http-functions';
import wixData from 'wix-data';
export async function get_deleteSellAndBuyDatabaseOldData() {
let FromMonthToDelete = 3; // Add the month here(Change)
let itemsToDelete;
let FromMonthToDeleteAgoDate = new Date(
new Date().getFullYear(),
new Date().getMonth() - FromMonthToDelete,
new Date().getDate()
);
let returnoptions = {
"headers": {
"Content-Type": "application/json"
}
};
let options = {
"suppressAuth": true,
"suppressHooks": true
};
var q1;
var q2;
q1 = await wixData.query("Sell_And_Buy")
.lt("_createdDate", FromMonthToDeleteAgoDate)
.find(options)
.then((results) => {
if (results.items.length > 0) {
itemsToDelete = results.items;
for (let i in itemsToDelete) {
wixData.remove("Sell_And_Buy", itemsToDelete[i]._id, options);
}
returnoptions.body = {
"Sell And BUY Database Status": "Items Deleted = " + results.items.length
};
return ok(returnoptions);
}
returnoptions.body = {
"Sell And BUY Database Status": "No Data To Delete"
};
return notFound(returnoptions);
})
// something went wrong
.catch((error) => {
returnoptions.body = {
"Sell And BUY Database Status": error
};
return serverError(returnoptions);
});
q2 = await wixData.query("Events")
.lt("_createdDate", FromMonthToDeleteAgoDate)
.find(options)
.then((results) => {
if (results.items.length > 0) {
itemsToDelete = results.items;
for (let i in itemsToDelete) {
wixData.remove("Events", itemsToDelete[i]._id, options);
}
returnoptions.body = {
"Events Database Status": "Items Deleted = " + results.items.length
};
return ok(returnoptions);
}
returnoptions.body = {
"Events Database Status": "No Data To Delete"
};
return notFound(returnoptions);
})
// something went wrong
.catch((error) => {
returnoptions.body = {
"Events Database Status": error
};
return serverError(returnoptions);
});
return [q1,q2];
///return q1 works
///return q2 works
//but return [q1,q2] is not working
}
Hello Nithin.
http function has to return ok(), or one of different functions, or promise with ok() (or different functions like serverResponse etc) at the end.If you return just q1 you return ok() for example, but if you return [q1,q2] return will be [ok(),ok()] which is not valid.
What you need is promise.all Promise.all() - JavaScript | MDN
Hello Andri,
Thanks for your reply. i tried your solution and it is still not working. I changed return [q1,q2]; to
return Promise.all(q1,q2);. Is this correct or am I doing anything wrong?
Not really. Promise.all takes an array of promises as a parameter.
//with async await
try{
const response = await Promise.all([q1,q2]);
const responseOfq1 = response[0];
const responseOfq2 = response[1];
//do stuff to responses
return ok({q1,q2});
} catch(e){
return serverError({error:e});
}
//without async await
return Promise.all([q1,q2]).then(values=>{
//do stuff to responses
return ok({q1,q2});
}).catch(e=>{return serverError({error:e)});
But you need to rewrite your code a little bit, because there is no need to return ok/ serverError etc inside of q1 and q2.
Hi Andri,
Thanks for your code and help. But now calling the http function both ways(async and normal ) is not creating any error but I am seeing a white screen instead of the reply that I am expecting
( Sell And BUY Database Status": “Items Deleted = " + results.items.length )
( Events Database Status”: "Items Deleted = " + results.items.length )
I tried returning return ok({q1,q2}) and return ok({ responseOfq1 , responseOfq2 }) and both are showing me a white screen 
Here is my final code:
import {
notFound,
ok,
serverError
} from 'wix-http-functions';
import wixData from 'wix-data';
export async function get_deleteSellAndBuyDatabaseOldData() {
let FromMonthToDelete = 3; // Add the month here(Change)
let itemsToDelete;
let FromMonthToDeleteAgoDate = new Date(
new Date().getFullYear(),
new Date().getMonth() - FromMonthToDelete,
new Date().getDate()
);
let returnoptions = {
"headers": {
"Content-Type": "application/json"
}
};
let options = {
"suppressAuth": true,
"suppressHooks": true
};
var q1;
var q2;
q1 = await wixData.query("Sell_And_Buy")
.lt("_updatedDate", FromMonthToDeleteAgoDate)
.find(options)
.then((results) => {
if (results.items.length > 0) {
itemsToDelete = results.items;
for (let i in itemsToDelete) {
wixData.remove("Sell_And_Buy", itemsToDelete[i]._id, options);
}
returnoptions.body = {
"Sell And BUY Database Status": "Items Deleted = " + results.items.length
};
return ok(returnoptions);
}
returnoptions.body = {
"Sell And BUY Database Status": "No Data To Delete"
};
return notFound(returnoptions);
})
// something went wrong
.catch((error) => {
returnoptions.body = {
"Sell And BUY Database Status": error
};
return serverError(returnoptions);
});
q2 = await wixData.query("Events")
.lt("_updatedDate", FromMonthToDeleteAgoDate)
.find(options)
.then((results) => {
if (results.items.length > 0) {
itemsToDelete = results.items;
for (let i in itemsToDelete) {
wixData.remove("Events", itemsToDelete[i]._id, options);
}
returnoptions.body = {
"Events Database Status": "Items Deleted = " + results.items.length
};
return ok(returnoptions);
}
returnoptions.body = {
"Events Database Status": "No Data To Delete"
};
return notFound(returnoptions);
})
// something went wrong
.catch((error) => {
returnoptions.body = {
"Events Database Status": error
};
return serverError(returnoptions);
});
try {
const response = await Promise.all([q1, q2]);
const responseOfq1 = response[0];
const responseOfq2 = response[1];
//do stuff to responses
return ok({
responseOfq1,
responseOfq2
});
} catch (e) {
return serverError({
error: e
});
}
}
You need to rewrite your code, a little bit. Don’t return ok/serverError or any other function call inside of q1 or q2. You can return result.items of q1 and q2 or any other data. Only after promise.all you will be able to return ok/serverError etc. Use this code as a example https://jsfiddle.net/rvbfs6a0/12/