Solved: WixData.Insert runs even when it shouldn't

I’m trying to pull data from a collection with my function VacantPremPick() (in the yellow box) by either getting data from an already existing row or creating a new row and using the data from that but even when I get the data from an already established pick, the other part of the if statement seems to run.
The screenshot of the console shows that the first part of the if statement runs as “pick already created” is there. However, the screenshot of the collection shows that a new row was created but the only place with wixdata.insert is in the else part of the if statement in the yellow box - furthermore none of the console.logs in the else block run.
Few things I’ve tried:
- Using a different functions for the insert and both parts of the if statement
- Used a counter to see how many times the function runs (only ever once)
- Restructured the function

I'm pretty new to velo and js and I've spent 4 days trying to solve this and i know it'll be a stupid logic error but i just can't find it. 

It’s hard to follow the full flow of your code from a screenshot. Perhaps the insert is being called in a way that you aren’t expecting.

Note that you are not properly handling the Promise returned from the query in line 35. You can’t use the .then() function together with the await keyword. It’s one, or the other. See the following for more information about Promises:

I would suggest adding some additional console.log() statements to see exactly when, when, and where things are happening.

It’s hard to follow the flow of your code from a screenshot. Perhaps the insert is being called in a way that you aren’t expecting.

Note that you are not properly handling the Promise returned from the query in line 35. You can’t use the .then() function together with the await keyword. It’s one, or the other. Using both will result in unpredictable results. See the following for more information about Promises:

I would suggest adding some additional console.log() statements to see exactly when, when, and where things are happening.

Thanks Yisrael! I’ve fixed it now and the problem was the fact I was using then and await - I just hadn’t looked deep enough into promises before I started.