I would appreciate if someone can help me and take a look at the code below and let me know where the error is.
Background:
I’m looking to add an afterQuery hook to a data collection called Visit so that I can add a “calculated” field (i.e. restoName) to my visitDataset .
Visit is a collection that has a refence field to Menu collection (i.e. menuChoice) , Menu has a reference field to Dish collection (i.e. dishId), and Dish has a reference field to Restaurant collection (i.e. dishResto).
The code below is adding restoName to the item, but the value appears as undefined.
Seems like the query result is disconnected from the rest of the function.
I tried assigning item.restoName = myRestoName and return item inside the query but nothing happens. As if the query is not executing?
Many thanks in advance,
JP
//Code from data.js
import wixData from ‘wix-data’;
export function Visit_afterQuery(item, context) {
var dishId = item.menuChoice.dishId; // menuChoice is a reference field pointing to "Dish"
var myRestoName;
wixData.query("Dish") // return the restaurant name for the item on "Visit"
.include("dishResto") // dishResto is a reference field pointing to "Restaurant"
.eq("_id",dishId)
.find()
.then((res) => {
myRestoName = res.items[0].dishResto.restoName;
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
});
item.restoName = myRestoName; // Add a calculated field "restoName"
console.log(item);
return item;
The query is executing, but you’re not waiting for it to finish before using its results. This is a Promise issue.
This code you’ve written:
item.restoName = myRestoName; // Add a calculated field "restoName"
console.log(item);
return item;
Will execute before the query is done (or more technically, before the Promise returned by find() has resolved).
What you need to do is stick that stuff in the then() and return the whole Promise. Something like this:
import wixData from 'wix-data';
export function Visit_afterQuery(item, context) {
var dishId = item.menuChoice.dishId; // menuChoice is a reference field pointing to "Dish"
var myRestoName;
return wixData.query("Dish") // return the restaurant name for the item on "Visit"
.include("dishResto") // dishResto is a reference field pointing to "Restaurant"
.eq("_id",dishId)
.find()
.then((res) => {
myRestoName = res.items[0].dishResto.restoName;
item.restoName = myRestoName; // Add a calculated field "restoName"
console.log(item);
return item;
})
.catch( (error) => {
let errorMsg = error.message;
let code = error.code;
});
}
We’re almost done writing an article about this issue, so keep your ears open. When you read it, we would appreciate any feedback you have.
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;
});
}