Hi,
I'm trying to perform a query and store the results to a variable outside the scope of the query, but inside function the code sits within.
If I console log the results (as pointed to by the variable) it works within the query.
However, if I log the results outside the query (using the same variable) it comes back as undefined.
This looks like a scope question. I create the queryresults variable outside the query and it sets okay within the query, but then loses it again outside the query.
Can anyone help please ?
Here's the code:
let queryresults; let query = wixData.query('Stores/Products');
query.find().then(res => {
queryresults=res; console.log("queryResults="+queryresults); <<Returns "queryResults=[object Object]" in log << }) .catch(err => { console.log("problem in search! " + err); });
console.log("queryResults="+queryresults); <<Returns "queryResults=undefined" in log <<
Aaah, so I should add an await on the query. Tried that and it works ! Thank you J.D.
Your last line runs before the promise gets resolved.
This is how it's expected to work.