async/await function returning undefined

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.

Dashboard page:

My code does the following:

  1. save input fields to Collection 1
  2. query Collection 1 for product id to retrieve ID
  3. 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.

Thanks in advance :slight_smile:

Try:
async function getAffID () {
return await wixData.query( “Affiliate_OT” ) // <= this return from getAffid()
.eq( “title” , titleAff)
.distinct( “_id” )
.then((res) => {
let idResults = res.items[ 0 ];
console.log( "this is the aff id query result: " , idResults);
return idResults; // <==== This return is from the then() arrow function.
});
}

Consuming promise is the way.

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.

Thanks again for taking the time to look at it.

let titleAff = $w ( ‘#inputAddProdID’ ). value ;

function getAffID () {
return 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 ()
.then(result => { console . log ( "results from get aff id: " , idResults )} ;

If idResult is you need to consume it.
I think asyc/await was creating another Promise.

You just need to await getAffId as well on your code. Any async function returns a promise. So you need to await for it to get the result.

let idResults = await getAffID();

Thank you mvveiga and niko barli for your replies. I have tried your suggestions but am still struggling.

I have also reviewed the documentation including this article:
Velo: Working with Promises

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?

thanks in advance!

How is structured your DATABASE ?
Can you show an little section of your database?

Hi russian-dima :slight_smile: thank you for your response. I am trying to retrieve an id from the Affiliate_OT collection (here is a screenshot):

and then update the Catalog collection “key” field with a reference to the id in affiliate_OT. here is the catalog collection:

but the value in “key” field is storing “undefined” because i am having trouble waiting on the results of the affiliate_OT id query.

any thoughts? -I am going a bit nuts :slight_smile:

@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.

  1. You have an inputfield, for example [INPUT1].
  2. In that field you put …
  3. 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.

This could also be useful…
https://www.wix.com/velo/forum/tips-tutorials-examples/how-to-get-data-from-referenced-collections

I was onced asking this behavior here:

https://www.wix.com/velo/forum/community-discussion/consistency-model-of-collection-read-write

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.

Thank you @giri-zano and @nikobarli - thank you both for your responses.

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?

Thanks so much for your responses :slight_smile:

@amy_meyers show us the code, then we can help you better

@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.