# Simultaneously Run  a query

**URL:** <https://forum.wixstudio.com/t/simultaneously-run-a-query/6787>\
**Category:** Ask the community\
**Tags:** question, wix-stores, code\
**Created:** [March 18, 2021, 11:19pm UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787 "2021-03-18T23:19:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![paiysurv](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paiysurv/32/3707_2.png) [@paiysurv](https://forum.wixstudio.com/u/paiysurv)\
**Post date:** [March 18, 2021, 11:19pm UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787/1 "2021-03-18T23:19:23Z")

</div>

I have been wondering how I can simultaneously run two queries in two different collections. I have tried promises but they seem not to work. Here is my code;

import wixData **from** ‘wix-data’ ;  
$w.onReady( **async function** () {  
wixData.query( “Billing/Invoices” )  
.eq( “status” , “Paid” )  
.find()  
.then( (result) =\>{  
**let** res=result.length  
wixData.query( “Stores/Orders” )  
.find()  
.then( (results) =\> {  
**let** cust=results.length  
**if** (res\> 0 ||cust\> 0 ){  
$w( ‘#group1’ ).show()  
$w( “#text10” ).hide()  
} **else** {  
$w( ‘#group1’ ).hide()  
$w( “#text10” ).show()  
$w( “#text10” ).text= “This page is accessible to non-first-time customers. Kindly place an order to get access to this page.”

```
} 

```

} );  
})})  
Any help will be appreciated!

---

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [March 19, 2021, 1:59am UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787/2 "2021-03-19T01:59:25Z")

</div>

At first glance, there are a couple of things wrong:

1. you do not need to declare the function async if you are not using await. Delete it
2. the returned result of a query is an object, with , amongst others, 1 key called “items”, which holds an array of rows. That’s the “real” data. So you should test for result.items.length.
3. you could have easily found this out yourself if you just put in a console.log or console.info here and there. That’s what they are made for: debugging.

Happy hunting

---

<div class="post-metadata">

**Author:** ![paiysurv](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paiysurv/32/3707_2.png) [@paiysurv](https://forum.wixstudio.com/u/paiysurv)\
**Post date:** [March 19, 2021, 6:59am UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787/3 "2021-03-19T06:59:37Z")

</div>

Thank you for your response on the hunt! I have removed the async and populated results as result.items.length. When I quey once collection only, everything works. The issue must be querying the two collections simultaneously. There are no errors on the console.log preview but the code doesn’t work on the live editor.

**import** wixData **from** ‘wix-data’ ;  
$w.onReady( **function** () {  
wixData.query( “Billing/Invoices” )  
.eq( “status” , “Paid” )  
.find()  
.then( (result) =\>{  
**let** res=result.items.length  
wixData.query( “Stores/Orders” )  
.find()  
.then( (results) =\> {  
**let** orders= results.items.length  
**if** (res\> 0 ||orders\> 0 ){  
$w( ‘#group1’ ).show()  
$w( “#text10” ).hide()  
$w( “#button1” ).hide()  
} **else** {  
$w( ‘#group1’ ).hide()  
$w( “#text10” ).show()  
$w( “#button1” ).show()  
$w( “#text10” ).text= “To access this page, please purchase a package.” ;  
}  
} );  
})  
})  
Have a look again. Regards!

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [March 19, 2021, 7:08am UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787/4 "2021-03-19T07:08:50Z")

</div>

**Giri** the **async** keyword convert the function to return a promise instead of **void** , also, the query result has a " **length**" property showing how many items the " **items**" property carry into it, and a " **totalCount**" to show the total matching results in the DB.

But you’re right about the **async** in this case since the query returns a Promise anyway.

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [March 19, 2021, 7:23am UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787/5 "2021-03-19T07:23:16Z")

</div>

Hey there 🖐

You don’t need something special to let the code run at the same time, just don’t wait for the promise to resolve.

```javascript
$w.onReady(() => {
    // Promise #1
    wixData.query('collection1').find();
    
    // Promise #2
    wixData.query('collection2').find();
})

```

If you want to wait for both results before continue.

```javascript
$w.onReady(() => {
    Promise.all([
        wixData.query('collection1').find(),
        wixData.query('collection2').find()
    ]).then((results) => {
        const firstResult = results[0];
        const secondResult = results[1];
    })
})

```

If you want to run them one after another

```javascript
wixData.query('collection1').find().then(() => {
    wixData.query('collection2').find()
})

```

Hope this helps~!  
Ahmad

---

<div class="post-metadata">

**Author:** ![paiysurv](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paiysurv/32/3707_2.png) [@paiysurv](https://forum.wixstudio.com/u/paiysurv)\
**Post date:** [March 19, 2021, 6:03pm UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787/6 "2021-03-19T18:03:51Z")

</div>

Hello Ahmad, the problem seems to be emanating from getting the results of both queries. I have tried with one query and everything seems to be working fine. but when I add the second query, it doesn’t work.  
For the sample code that you sent, I changed to match my requirements:

**import** wixData **from** ‘wix-data’ ;  
$w.onReady(() =\> {  
Promise.all([  
wixData.query( “Billing/Invoices” )  
.find(),  
wixData.query( “Stores/Orders” )  
.find()  
]).then((results) =\> {

**const** firstResult = results.length;  
**const** secondResult = results.length;  
**if** (firstResult\> 0 ||secondResult\> 0 ){  
$w( ‘#group1’ ).show()  
$w( “#text10” ).hide()  
$w( “#button1” ).hide()  
} **else** {  
$w( ‘#group1’ ).hide()  
$w( “#text10” ).show()  
$w( “#button1” ).show()  
$w( “#text10” ).text= “To access this page, please purchase a package.”  
}})})  
Any other suggestions on where I might be doing it wrong?

---

<div class="post-metadata">

**Author:** ![mvveiga](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mvveiga/32/122599_2.png) [@mvveiga](https://forum.wixstudio.com/u/mvveiga)\
**Post date:** [March 19, 2021, 7:48pm UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787/7 "2021-03-19T19:48:24Z")

</div>

I guess the issue is Promises, async and returns.  
You can try:

**import** wixData **from** ‘wix-data’ ;  
$w.onReady( **function** () {  
wixData.query( “Billing/Invoices” ).eq( “status” , “Paid” ).find()  
.then((result) =\> {  
**let** res = result.items.length  
**return** wixData.query( “Stores/Orders” ).find() // Return  
.then((results) =\> {  
**let** orders = results.items.length  
**if** (res \> 0 || orders \> 0 ) {  
$w( ‘#group1’ ).show()  
$w( “#text10” ).hide()  
$w( “#button1” ).hide()  
} **else** {  
$w( ‘#group1’ ).hide()  
$w( “#text10” ).show()  
$w( “#button1” ).show()  
$w( “#text10” ).text = “To access this page, please purchase a package.” ;  
}  
**return** // Return  
});  
})  
})

OR using closure ()

**import** wixData **from** ‘wix-data’ ;  
$w.onReady( **function** () {  
wixData.query( “Billing/Invoices” ).eq( “status” , “Paid” ).find()  
.then((result) =\> {  
// Closure  
**let** res = result.items.length  
**function** queryStore() {  
return wixData.query( “Stores/Orders” ).find()  
.then((results) =\> {  
**let** orders = results.items.length  
**if** (res \> 0 || orders \> 0 ) {  
$w( ‘#group1’ ).show()  
$w( “#text10” ).hide()  
$w( “#button1” ).hide()  
} **else** {  
$w( ‘#group1’ ).hide()  
$w( “#text10” ).show()  
$w( “#button1” ).show()  
$w( “#text10” ).text = “To access this page, please purchase a package.” ;  
}  
});  
}  
queryStore() // for anonymous function you can to use (function() {})()  
//Closure  
})  
})

wixData.query is a promise.

---

<div class="post-metadata">

**Author:** ![giri-zano](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/giri-zano/32/80469_2.png) [@giri-zano](https://forum.wixstudio.com/u/giri-zano)\
**Post date:** [March 20, 2021, 5:59pm UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787/8 "2021-03-20T17:59:31Z")

</div>

@ahmadnasriya You’re right. Am so used to always getting the length from the array, that I forgot that one. Thanks for reminding.

---

<div class="post-metadata">

**Author:** ![paiysurv](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paiysurv/32/3707_2.png) [@paiysurv](https://forum.wixstudio.com/u/paiysurv)\
**Post date:** [March 21, 2021, 3:07am UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787/9 "2021-03-21T03:07:07Z")

</div>

Hello MVVeiga, thanks for the effort. Unfortunately the code too does not work. The query for the invoices works well but the Orders query seems to fire blank.

---

<div class="post-metadata">

**Author:** ![paiysurv](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/paiysurv/32/3707_2.png) [@paiysurv](https://forum.wixstudio.com/u/paiysurv)\
**Post date:** [March 21, 2021, 3:07am UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787/10 "2021-03-21T03:07:07Z")

</div>

Hello MVVeiga, thanks for the effort. Unfortunately the code too does not work. The query for the invoices works well but the Orders query seems to fire blank.

---

<div class="post-metadata">

**Author:** ![mvveiga](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mvveiga/32/122599_2.png) [@mvveiga](https://forum.wixstudio.com/u/mvveiga)\
**Post date:** [March 21, 2021, 2:46pm UTC](https://forum.wixstudio.com/t/simultaneously-run-a-query/6787/11 "2021-03-21T14:46:01Z")

</div>

@paiysurv I guess the issue is not about simultaneous queries. It is seeming the query “Stores” has no result.  
Console the results from ‘billing’ and ‘store’ inside each query.
