Hello,
I just want to be able to store the result of a query in a variable and unfortunately it did not work.
I know the query is finding a value because when I put a console.log inside the query it is working.
But unfortunately when I move the console.log outside, it is not working…
let airportFrom = $w("#selectionAirportFrom").value;
let itemAirport;
wixData.query("airportDataset")
.eq("airportName", airportFrom)
.find()
.then((results) => {
let items = results.items;
itemAirport = items[0].oaci;
})
.catch((err) => {
let errorMsg = err;
console.log(errorMsg);
});
console.log(itemAirport); /// -> ItemAirport is not visible in the console
Thanks
Pym
How did you end up writing this?
Hi,
Can you please send the site URL and the page name to which the code was added so that I can have a look?
Roi
When inserting one line, I have to get in the same time 2 fields from the same reference table
“From” field is selected by the client and I have to fill with the referenced OACI value
“To” field is selected by the client and I have to fill with the referenced OACI value
Thus I will have to manage 2 imbricated wix.result, too complicated for me.
I think the easiest way is to first insert the line and then via a hook update the 2 fields sequentially.
Not sure it will work...
Thanks for your Yisrael
pym
Sorry, I don't quite understand your insert logic.
The idea is that my wife never quite knows when I'll take out the trash. And, you can't say exactly when the wix-data query will return its results - although wix-data is more reliable and responsive than I am. Your logic has to be structured so that things happen asynchonously - that is, non-sequentially. So, you need to include the other stuff you want to do inside the .then.
You can see some examples how this is done in the article How to Use Code to Let Your Users Search a Collection.
Hi Yisrael,
Manu thanks for your explanation !!
I understood the logic.
The problem is I want to be able to store the value to use it in an insert statement in a second hand…
if ($w("#bookingRadioGroup").value === "roundTrip") {
// Leg 1
let flightLeg1_toInsert = {
"flightStatus": "submitted",
"from": XXXX // -> The value of the query,
…
Does it mean I have to include the result of my query inside the above insert ?
Thanks
Pym
Hi Pym,
It's actually working. The query returns the result, when, and only when the Promise has been fulfilled. Look at it this way...
My wife asks me to take out the trash (the query)
I say, "yes dear" (the promise)
Wife doesn't expect this to happen any time soon, so she keeps on doing whatever it is she's doing. That is, outside.
Eventually (after I've had a beer or two) I take out the trash (now we're inside the .then)
So, as you can see, after my wife asks me to take the trash, if she was to do a console.log(trash taken out) "outside", it would come up empty.
This little story illustrates that you need to do whatever it is you want to do "inside" - that is, inside the .then.
I hope this helps. (and don't tell my wife)
Yisrael