Inconsistent output when querying a dataset

I have a function that receives a user id and it searches the “orders” dataset and retrieves all the orders for that user and does some simple calculation on it and returns the result. when I call this function multiple times in a row with different user id each time, after 2nd or 3rd or 4th time (its not consistent, though I’ve seen it complete) it wont return anything and the rest of the script seems to just crash. Even if I remove the filters on it and run it, I still get similar result.

Here is an example of how I am calling the function.

 await getOrderTotal(user1_id)
                     console.log("1 ok");
 await getOrderTotal(user2_id)
                     console.log("2 ok");
 await getOrderTotal(user3_id)
                     console.log("3 ok");
 await getOrderTotal(user4_id)
                     console.log("4 ok);
            

Here is the function

async function getOrderTotal(userId){
 
 return await wixData.query("Stores/Orders").eq("buyerInfo.id", userId).find()
 .then( (result)=> {
        console.log(result);
        let sum = 0
        if(result.length > 0){
        for (let i = 0; i < result.length; i++) {
               const total = result.items[i].totals.subtotal
               sum += parseInt(total)
        }
       }
       else{
            return 0
       }

 return sum
 }).catch((err) =>{
         console.log(err);
 return 0
 })
 }

Here is the output running the the program back to back with out making any changes. if you see the first time it only output “ok” 3 times and didn’t finish the program. second time it output “ok” 4 times and finished properly.

Thank you for your help!

You can’t use await together with the Promise’s .then() function. Also, you need to return sum in the correct place in the code, in the .then() function, where the value is available.

See the following for more information about Promises:

If you prefer using await , then you should not use the .then() function.
See the article on async/await for more information.

Using .then() , you want something like this:

function getOrderTotal(userId) {
   return wixData.query("Stores/Orders")
   .eq("buyerInfo.id", userId)
   .find()
   .then((result) => {
      console.log(result);
      let sum = 0
      if (result.length > 0) {
         for (let i = 0; i < result.length; i++) {
            const total = result.items[i].totals.subtotal;
            sum += parseInt(total);
         }
         return sum; // return here inside the .then() function
      }
      else {
         return 0;
      }
   }).catch((err) => {
      console.log(err);
      return 0
   })
}

Disclaimer: This hasn’t been tested, but should give you an idea of what needs to be done.

you need result.items.length instead of result.length

It’s actually the same, getting the length of the items array will give you the same result as getting the length from the result object, the length property of the result is calculated by the length of the array anyway.

Thank you for your response. I had no idea about using async/await and then() separately. It always worked for me thus far so I just kept using it. The article helped alot.
I have narrowed down my problem to that its how I am calling the function. when calling the function I am using await . if I do call the function without the await keyword then I get the output for all the user_ids and now I can work my solution around that. though not sure why

Thank you