Query within Hook doesn't work! Why?

Hello, people!

Is it possible someone help me on this?

I’m trying to get the number of items on Collection before insert a new item, to set the ID properly.

So I created a Hook to do this. When I run it without a query, it works (the Hook is configured properly and the permissions as well). But when I run a query inside the Hook to get the count, the item is inserted with the field empty.

I realized that we don’t need to return the item, it’s just an option, because when I set the new ID property whitout a query, and not returning the item, it has been inserted ok.

So, I think, as a query waits a promise, maybe the item is being inserted before this resolution.

How can I resolve this?

After some researchs about javascript async functions, I could solve this problem adding async to the function´s declaration, try/catch inside and await on query. I hope it´s gonna be usefull to someone else.

1 Like

Thank you for your generosity in sharing the solution. Saved my life.

God bless you!