Trying to assign value from a array inside a repeater textbox

Hello
I’ve been trying for a day to get the result of a loop put into a repeater.

I have a dataset that contains producers (Title,Last name,First name,multi reference (products))
and a second dataset which contains the products and the producers’ references

with the following code I manage to retrieve for each producer a list of products.

// Pour une documentation complète sur API, incluant des exemples de code, allez sur http://wix.to/94BuAAsimport wixData from 'wix-data';
import wixData from 'wix-data';
$w.onReady(function () {

  wixData.query("Producteurs")
    .include("produit")
    .find()
    .then((results) => {
      console.log(results);
 let produitlist = "";
      results.items.forEach((item) => {
        console.log(item);
 
 for (var i = 0; i < item.produit.length; i++) {
          produitlist = produitlist + " " + item.produit[i].title.replace('\n', '');
        }
        console.log(produitlist)
        $w("#produitlist").text = produitlist;
        produitlist = "";
      });

    });
});

The problem is that during the display, it is always the list of products of the last producer that is displayed.

What i’m missing ?

kind regards

Anyone ?

The short answer is that in a repeater, you need to use the at() function to be able to assign a value to a particular item in a repeater.
https://www.wix.com/corvid/reference/$w/at

Take a look at the section entitled Repeated Item Scope in the repeater introduction.
https://www.wix.com/corvid/reference/$w/repeater/introduction

Since you are running a query, it’s typical to assign the result to the data property of the repeater. I don’t see where you’re doing that.
https://www.wix.com/corvid/reference/$w/repeater/data

I would also spend some time on what the onItemReady and forEachItem functions’ purpose and common use is. What you’re trying to do is probably better handled by these repeater functions designed to deal with populating repeater items rather than a standard javascript forEach loop. I say that not understanding exactly what you’re attempting to do …

@tony-brunsman
I’ll take a look at all of this, thank you for the answer.

I try to acheve :
Each producer can have 1 to X type of product.
For that I use a multi reference field.

For example: Fruit,vegetable,meat

I would like to be able to display all the elements as a text field.
“Fruit vegetable meat”
The loop is used to concatenate the values of the list to have only 1 string.

But i’m not a JS guru so i do my best atm

Dunno exactly why but this code works

import wixData from 'wix-data';
$w.onReady(function () {
  $w("#dataset1").onReady(() => {
    $w("#producteurs").forEachItem(($item, itemData, index) => {

      wixData.queryReferenced("Producteurs", itemData._id, "produit")
        .then((results) => {
 //console.log(results.totalCount)
 let value = "";
 for (var i = 0; i < results.totalCount; i++) {

            value = value + " " + results.items[i].title.replace('\n', '');
 //console.log(results.items[i].title)
 //console.log(value)
          }
          $item("#produitlist").text = value;
        })
        .catch((err) => {
 let errorMsg = err;
        });

    });
  });

});