Need help with afterQuery hook

Hi @Sam,

Your comment is almost exactly what I’m looking for, except that in the “then” part of your code, I want to execute a wixData.update() on a different table, and right now I can’t get it to work. I would much appreciate it if you could help me - it’s in the 2nd asynchronous part (the wix.update() part) that it doesn’t work. I tried removing the “return” keyword from that line too and it still didn’t work.
Thanks in advance!
This is my code:

export async function Bookings_beforeInsert(item, context) {
return wixData.query(“Classes”)
.eq(“title”, item[“title”])
.find()
.then((res) => {
if (res.items.length > 0) {
console.log(res.item[0]);
let firstItem = res.items[0];//First item in results
firstItem.takings += item[“cost”]; // updated class takings
firstItem.profit += item[“cost”]; // updated class profit
return wixData.update(“Classes”, firstItem)
.then( (updatedItems) => {
return item;
} )
. catch ( (err) => {
let errorMsg = err;
} );
} else {
return item;
}
})
. catch ( (error) => {
let errorMsg = error.message;
let code = error.code;
});
}