[resolved] how to use query with include in repeater?

I use the following code to feed a repeater with the data of “Events” and “Companies” :

in table “Events”, the field “compagnie” is a reference of table “Compagnies” with primary field “title”

My code is :

import wixData from ‘wix-data’ ;
import { debounce } from ‘lodash’ ;

$w.onReady( function () {
initRepeater();
buildFiltersAndPopulateRepeater();
});

async function buildFiltersAndPopulateRepeater() {
let dataQuery = wixData.query( ‘Events’ ).include( ‘Compagnies’ );
// EvenementListe is my repeater name
$w( ’ #EvenementListe ’ ).data = await dataQuery.find().then((res) => res.items);
}

function initRepeater() {
$w( ’ #EvenementListe ’ ).onItemReady(($item, itemData) => {
// pDate, pHeure, pLieu, pTitre… are text and buton elements in repeater
// this code works correctly
$item( ’ #pDate ’ ).text = itemData.dateAffiche;
$item( ’ #pHeure ’ ).text = itemData.heureAffiche;
$item( ’ #pLieu ’ ).label = itemData.salleAffiche;
$item( ’ #pTitre ’ ).text = itemData.title;
$item( ’ #pSousTitre ’ ).text = itemData.infoSpcialeSoustitre;
$item( ’ #pChoregraphe ’ ).text = itemData.chorgraphe;
$item( ’ #pDetail ’ ).text = itemData.dtail;
// this code that doesn’t work : syntax ?
$item( ’ #pCompagnie ’ ).label = itemData.Compagnies.title ;
}
)}

Can you help me ?

I do not know filter ----> “include”

Take a look at this…

https://www.wix.com/corvid/reference/wix-dataset/dataset/setfilter

Thanks, but I have no problem with the filter, nor with the sort, but only with the syntax to use to read the data from the include table.

oups, there is an error in the code above: you should read this:

let dataQuery = wixData.query( ‘Events’ ).include( ‘Compagnies’ );

$w( ’ #EvenementListe ’ ).data = await dataQuery.find().then((res) => res.items);

where #EvenementListe is a repeater

the result is : if i use $item( ’ #pCompagnie ’ ).label = itemData.compagnie

I do not know the structure of your collections (" Events" , and “Compagnies” ).
You perhaps just reference to the wrong column of your “Compagnies” - Collection.

Sorry, i think i am not really able to help you.

Hello,
here is some additional information :

It would be better to know the all the name_ids of reference-fields in “Compagnies”.

For which COLUMN are you searching for in your “Compagnies”-DATABASE ?

You are refering here to title.

// it's the code that doesn't work : syntax  itemData.compagnies.title is wrong            $item('#pCompagnie').label = itemData.Compagnies.title  ;

Show on a pic the column you need. Is is really “title”, what you are searching for?

@russian-dima
yes, the column name is title

@bjar

Ok sorry, at the moment without any idea. Perhaps it is already a little bit to late to overload my little brain :grin:. I will give it a second try tomorrow. :sweat_smile:

@bjar

import wixData from 'wix-data';
import { debounce } from 'lodash';

$w.onReady(function () {
    initComps();
    initRepeater();
    buildFiltersAndPopulateRepeater();
});

async function initComps() {
 // populate iSalle dropdown 
 const res = await wixData.query('Events')
        .ascending('salleAffiche')
        .distinct('salleAffiche')
        .then((locationData) => {
 return locationData.items.map((location) => {
 return {
                    value: location,
                    label: location
                }
            });
        });
    $w('#iSalle').options = res;


 // bind all input elements
 const componentsTypeArray = ['RadioButtonGroup', 'Dropdown'];

 const debounceFunction = debounce(buildFiltersAndPopulateRepeater, 200);
    componentsTypeArray.forEach((compType) => {
 const compArray = $w(compType);
        compArray.forEach((comp) => {
            comp.onChange((event) => {
                debounceFunction();
            });
        });
    });
}

async function buildFiltersAndPopulateRepeater() {
 let dataQuery = wixData.query('Events').include('Compagnies');

 if ($w('#iSalle').value) {
        dataQuery = dataQuery.eq('salleAffiche', $w('#iSalle').value);
    }

 // dataQuery = dataQuery.ascending("date") ;

 if ($w('#iTri').value) {
        dataQuery = dataQuery.ascending($w('#iTri').value) ;
    }
    $w('#InfoPreparation').show() ;
    $w('#ImageSablier').show() ;
    $w("#EvenementListe").hide();
    $w('#EvenementListe').data = await dataQuery.include('Compagnies').find().then((res) => res.items);
    $w('#InfoPreparation').hide() ;
    $w('#ImageSablier').hide() ;
    $w("#EvenementListe").show();

}

function initRepeater() {
    $w('#EvenementListe').onItemReady(($item, itemData) => {
 //$item('#pImage').src = itemData.thumbnailImage;
        $item('#pDate').text = itemData.dateAffiche;
        $item('#pHeure').text = itemData.heureAffiche;
        $item('#pLieu').label = itemData.salleAffiche;
        $item('#pTitre').text = itemData.title;
        $item('#pSousTitre').text = itemData.infoSpcialeSoustitre;
 
        $item('#pCompagnie').label = itemData.organisateur;
        $item('#pChoregraphe').text = itemData.chorgraphe;
        $item('#pDetail').text = itemData.dtail;
 
        $item('#pDebug').text = String(itemData.typeDevent) ;

 if (itemData.compagnie) {


// it's the code that doesn't work : syntax  itemData.compagnies.title is wrong
   $item('#pCompagnie').label = itemData.Compagnies.title  ;
   
   //what is the OUTPUT-RESULT of this CONSOLE-CODE???
   console.log(itemData.compagnie)
   //you should get an object, right?
   
   //open the results do a screenshot. 
}

@russian-dima
with a clean code, it may be easier to understand :

import wixData from ‘wix-data’ ;
import { debounce } from ‘lodash’ ;

$w.onReady( function () {
initRepeater();
buildFiltersAndPopulateRepeater();
});

async function buildFiltersAndPopulateRepeater() {
let dataQuery = wixData.query( ‘Events’ ).include( ‘Compagnies’ );
$w( ’ #EvenementListe ’ ).data = await dataQuery.find().then((res) => res.items);
}

function initRepeater() {
$w( ’ #EvenementListe ’ ).onItemReady(($item, itemData) => {
// the following code use “Events” and it’s good
$item( ’ #pDate ’ ).text = itemData.dateAffiche;
$item( ’ #pHeure ’ ).text = itemData.heureAffiche;
$item( ’ #pLieu ’ ).label = itemData.salleAffiche;
$item( ’ #pTitre ’ ).text = itemData.title;
$item( ’ #pSousTitre ’ ).text = itemData.infoSpcialeSoustitre;
$item( ’ #pChoregraphe ’ ).text = itemData.chorgraphe;
$item( ’ #pDetail ’ ).text = itemData.dtail;

**// it's the code use "Compagnies" that doesn't work : syntax ???**  
    $item( ' [#pCompagnie](https://www.wix.com/corvid/forum/search/~num~pCompagnie) ' ).label = itemData.Compagnies.title  ;  

}

@bjar

Hmmm i tried to reconstruct your issue, but a couldn’t…:disappointed_relieved:

Run this code and take a look in CONSOLE at your RESULTS.
The RESULTS could help you to unterstand whats going on and how to fiy it.

async function buildFiltersAndPopulateRepeater() { 
 let dataQuery = wixData.query('Events').include('Compagnies'); 
     dataQuery.find()
    .then((results) => {
        console.log(results)
    })
} 

This is also very interessting, because this is exactly what you get…(marked in blue). You get also the ID and not a referenced value.

Another idea would be, just take this ID (which should represent the current item-ID) and do a second query with FILTER for this ID in “Compagnies” database.

wixData.query("Compagnies")
 .find()
 .eq ("THE RIGHT COLUMN WHERE TO SEARCH", HERE THE INCLUDED-ID)
 .then( (results) => {
   if(results.items.length > 0) {
     let firstItem = results.items[0]; //see item below
    } else {
      // handle case where no matching items found
    }
} )

This is all i can help you. Perhaps some else have better ideas, or even the right solution.

@russian-dima

to help better understand my problem, i simplify code in first post :nerd_face:

but I still haven’t found the solution…

@bjar
You will find your solution when you CONSOLE-LOG every step of your CODE.
Seems as you are not a total beginner.
The console-method will help you out of your issue.
Try to understand how the INCLUDE-COMMAND works and what it does exactly.

When i tried to reconstruct a little bit of your project, i got the same result.
I got an ID like you.

Perhaps one of the PROs can help here.
@Ahmad
@J. D.
@ Giri Zano
@ anthonyb

@russian-dima

Hello,

With your advice, I made several tests :

Test 1 :

 let dataQuery = wixData.query('Events').include('Compagnies'); 

console.log result :

I receive 50 items of Events : exemple :


and since field compagnie is defined as :

but i also receive this message

Test 2 :

i use field value of compagnie for query Compagnies

let dataQuery = wixData.query('Compagnies').eq('_id','facb7085-ebca-4e34-9cc3-fcd1aa0bae86');

i receive 1 item of Compagnies :

so there is a link between Events.compagnie and Compagnies._id

there is therefore an incomprehension in the use of include () ???

@bjar
Yes, this is the same RESULT what i got!
And i did not understand why. Perhaps someone can explain it.

@russian-dima
Thanks a lot for your help. This is the 1st project using WIX. Usually i use php with javascript, and i don’t have this type of issues, but the web design is more complex.
I will continue to research how include …

@bjar
I saw a post today, where someone also uses include, perhaps you search for this post. Should be on the first 2 or 3 forum sites right now. It was just today.
Take a look for it.

Or just try to search for similar issues, perhaps the wanswer for our question (ussue) is already given somewhere.

I finally found the solution. It is not the name of the second collection that must be used in include, but the name of the field in the primary collection


in my case, it was necessary to use :

let dataQuery = wixData.query('Events').include('compagnie');

where “compagnie” is key-field of “Events”, referring to collection “Compagnies”

function initRepeater() {
    $w('#EvenementListe').onItemReady(($item, itemData) => {
		...		
		$item('#btnCompagnie').label = itemData.compagnie.title ;
		}) ;
} ;

Well done!