I have a dashboard page with input fields meant to add new products to a custom product catalog. The product details span 3 collections connected with reference fields. When adding new products, I need to update all 3 collections.
save input fields to Collection 2 including ID (retrieved in step 2) to create the reference link between collection 1 & 2
The results of the query in step 2 sometimes returns the id and sometimes return undefined. So, I have changed the function to be async/await. But, the query is still returning undefined.
Here is my function:
let titleAff = $w('#inputAddProdID').value;
async function getAffID () {
await wixData.query("Affiliate_OT")
.eq("title", titleAff)
.distinct("_id")
.then((res) => {
let idResults = res.items[0];
console.log("this is the aff id query result: ", idResults);
return idResults;
});
}
let idResults = getAffID();
console.log("results from get aff id: ", idResults);
I am embarrassed to admit how long I have been working on this dashboard page. I would really appreciate any thoughts/advice.
Thank you so much for your response. I replaced my code with yours and unfortunately it is still passing back “promise<>”. No matter what I have tried, it just does seem to wait to resolve the query before passing the result.
Any other thoughts you may have would be greatly appreciated.
I am trying to query for an item id so that I can insert it as a reference into another collection. But, I can’t seem to get the code right so that it waits for the id.
I apologize if it is my inexperience.
Does anyone have sample code or suggestions of how i can accomplish getting the item id prior to the insert reference?
@amy_meyers
Ok, i think you have to explain a little bit more.
Your DBs and your code, are a little bit confusing for me.
I can not recognize the relationship between the two shown DBs.
What is the common denominator between the shown DBs?
How exactly should this work?
You have 2-DBs —> [1] Affiliate_OT and your Catalog where you have all your items inside [2] “Catalog”.
How these two are connectec to eachother?
I also can see a “Multireference-Field”.
Please, describe your steps.
You have an inputfield, for example [INPUT1].
In that field you put …
…
and so on…
I need first to understand the context between the two shown DBs, before i can help you.
May I try to offer a different cause: when you write something to a collection and immediately query it, you will run up against a “lag”: the .save returns of fulfilled promise, but it still has not been written fully to (distributed) collection.
There is only 1 solution: build a while loop which retries 5 times and inside the loop, wait for 200 ms, try query again, until you have something. Usaully you have it within 1 or 2 retries.
It seems that Wix has “fixed” this. My test site (linked in the above thread) and also my production site no longer suffer from this behavior as of today. But yes, I am not sure if this is the correct programming model that Wix intended, or if the behavior is just by chance.
What you both describe seems to be the problem that I am currently having. I have been playing with adding the while loop but it still seems that I am only getting the correct id back sometimes. Other times, I am getting the id for the previous item in the collection.
I am really stuck. Any other thoughts on how I can go about this a different way?
@nikobarli Don’t count on it (that it is fixed). We discussed this problem quite recently (around the date of your original question) and it was still there. Replication lag cannot be avoided, you will just have to wait. In your case, it might just be luck.
It’s up to you, but I use a retry loop. We even discussed incremental or linear timeout inside the loop, and settled on linear (so 200 (or 250) ms per iteration, not 200, 400, 600, etc).
the amount of ms depends on which node in the CDN you’re on (US is fastest, 150 should do), but this makes code less portable. That’s why I do 200 or 250.