Pass multiple query results to repeater

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, 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

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 Rodney,

I’m also trying to combine multiple queries into one repeater and I’m having trouble getting it to work… Basically I have an “Event Photos” collection with a many-to-many relationship with a “Categories” collection. I just want users to be able to search for “Event Photos” and have the results also be filtered by the “Categories” reference field. So for example, if someone types “Corporate” into my search box, all event photos with the word “Corporate” in the Categories reference field would be returned. I really wish reference fields could be included in the same query!!

Any way, here’s what I have so far… Could you help me since you were able to make it work for yourself?

Thanks so much!!!

import wixData from ‘wix-data’;

$w.onReady( function () {
$w(“#searchedrep”).onItemReady(($item, itemData, index) => {
$item(“event”).text = itemData.relatedjob;
$item(“#description”).html = itemData.jobdescription.substring(0, 225) + ‘…’;
$item(“#thumbnail”).src = itemData.thumbnail;
$item(“#thumbnail”).link = itemData[‘link-EventPhotos-relatedjob’];
$w(“#thumbnail”).clickAction = “link”;
})
});
export function searchbutton_click_1(event) {
wixData.query(“EventPhotos”)
.contains(“relatedjob”,$w(“#searchbox”).value)
.find()
.then( (results) => {
let ResultsPic = results.items;

  wixData.query("Categories") 
  .contains("#title",$w("#searchbox").value) 
  .find() 
  .then( (results1) => { 

let ResultsCat = results1.items;
$w(“#searchedrep”).data = ResultsPic.concat(ResultsCat);
$w(“#searchedrep”).expand();
} );
} );
}

@elena Hi Elena, sorry for this late answer, I could take a look and help you out now, if you haven’t already solved it of course.

@rodney Hi Rodney,

I haven’t solved it yet! Thank you for responding. I would love your help. I’m not sure if concat is going to be the best solution for me, since I really only want results to show items from my events table (table 1) - I just want those items to be searchable by their referenced/related “categories” (table 2) as well. Thank you SO MUCH for your help!

@elena Elena try this:

export function searchbutton_click_1(event) {
wixData.queryReferenced(“EventPhotos”, $w(“#searchbox”).value, “Categories”)
.then((results) => {
let ResultsPic = results.items;
$w(“#searchedrep”).data = ResultsPic
$w(“#searchedrep”).expand();
})
. catch ((err) => {
let errorMsg = err;
});
}

I haven’t tested it, but let’s see if it works or at least gets you on the path. Please, check if “EventPhotos” and “Categories” are exactly written as your collection’s names. Let me know how it turns out!

@rodney

I gave it a shot, no results. No error either, but no results… This is what I have, did I make a mistake somewhere?

import wixData from ‘wix-data’;

$w.onReady( function () {
$w(“#searchedrep”).onItemReady(($item, itemData, index) => {
$item(“event”).text = itemData.relatedjob;
$item(“#description”).html = itemData.jobdescription.substring(0, 225) + ‘…’;
$item(“#thumbnail”).src = itemData.thumbnail;
$item(“#thumbnail”).link = itemData[‘link-EventPhotos-relatedjob’];
$w(“#thumbnail”).clickAction = “link”;
})
});

export function searchbutton_click_1(event) {
wixData.queryReferenced(“EventPhotos”, $w(“#searchbox”).value, “Categories”)
.then((results) => {
let ResultsPic = results.items;
$w(“#searchedrep”).data = ResultsPic
$w(“#searchedrep”).expand();
})
. catch ((err) => {
let errorMsg = err;
});
}

Hi @rodney , any ideas? I haven’t been able to make it work yet…