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!
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.
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.
$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
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