Hey jp,
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.