Hi! I'm running multiple queries for different databases/collections, and I need to pass them to one repeater. The closest I get is to send only the first array of results, but the others are not passed to (knew this could happen but had to try).
wixData.query("Casas")
.find()
.then( (resultsc) => {
$w("#repeater1").data = resultsc.items
});
wixData.query("DepartamentosV")
.find()
.then( (resultsd) => {
$w("#repeater1").data = resultsd.items
});
wixData.query("SuitesV")
.find()
.then( (resultss) => {
$w("#repeater1").data = resultss.items
});
I tried several times to change the code so it can make one single array from all the results like this one:
wixData.query("Casas")
.find()
.then( (resultsc) => {
let resultsC = resultsc.items;
wixData.query("DepartamentosV")
.find()
.then((resultsd) => {
let resultsD = resultsd.items;
wixData.query("SuitesV")
.find()
.then((resultss) => {
let resultsS = resultss.items
$w("#repeater1").data = [resultsc.items, resultsd.items, resultss.items]
});
});
} );
Nothing happened haha. Alternatively, I've also been looking on wix documentation and this forum for a way to run ONE query for all the databases I need, but only found something about a new feature wix is developing and it might do what I want. In the mean time, does anyone know a way to achieve this? Or maybe a workaround, using datasets and the .getCurrentItem but I think it would lead me to the same issue, which -in short- is to pass ALL the results to the repeater.
Any guidance would be really helpful! Thanks!
Hi Chiu, thanks I found that I could use .concat to merge all the results into one new array and now it works perfectly!
wixData.query("Casas") .find() .then( (resultsc) => { let ResultsCV = resultsc.items wixData.query("DepartamentosV") .find() .then( (resultsd) => { let ResultsDV = resultsd.items wixData.query("SuitesV") .find() .then( (resultss) => { let ResultsSV = resultss.items $w("#repeater1").data = ResultsCV.concat(ResultsDV, ResultsSV) } ); } ); } );
Yet I'm not confortable with the code, you see I need to run the query for 18 collections, and it will be quite large. I think it would be more efficient to loop through all of them, and I've been trying to do so since yesterday but I got a little confused and figured I should stop for some time. Anyway, today I'm going back to it with a fresh mind. I'm pasting the code (or the attempt of it haha) below so maybe you or someone can help me taking a look, maybe you come up with some idea.
const Collections = ["Casas", "DepartamentosV", "SuitesV"]; var Results = []; function runQuery (array) { wixData.query(Collections) .find() .then( (results) => { Results.concat(results.items); console.log(Results) }) } Collections.forEach(runQuery) .then (() => { $w("#repeater1").data = Results })
The logic behind that is:
1. "Collections" contains all the names (I'm only using three of them until I got the code right)
2. "Results" will contain every query result
3. I created a function that runs the query => I'm pretty sure here lies my confusion
4. I use .forEach to perform the runQuery function on each of the "Collections" elements and once it's done the repeater gets the results.
The error I get is that the query won't run, and it makes sense since I'm not able to tell it to pick just one of the "Collections" elements. I guess I need to create a for items loop inside the function too, I'll try that and see what happens. I'm not a coder so I might be using the wrong syntax, sorry! But this is my approach, if you or someone reading this think there is a better one it would be great if you could share it with me, or if you think this way should do the trick but help me spot whatever is it that I'm missing it's fine as well. Thanks!
Hi, I guess you can run several query and save to a named querY. Then, use Or command to combine into one query. Finally pass it to the repeater