Simultaneously Run a query

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!

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

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!

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.

Hey there :raised_hand_with_fingers_splayed:

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

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

If you want to wait for both results before continue.

$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

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

Hope this helps~!
Ahmad

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?

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.

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

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.

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.

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