Label of dropdown invisible :-)

hi,

I created a database with links to others database. Now I want to filter it and see only that I want.

I have this code below and my problem is that we can’t see the label in the dropdown (at the end of the buildOptions function). Is ok if I put two time “curr” but it’s the ID and I would want to see the name (field title of the database Autres_Escapes_Organisme).

Somebody can help me?

import wixData from "wix-data";

let lastFilterOrganisme;
let lastFilterLieu;
let title;

$w.onReady(() => {
 // query retourne toutes les valeurs des items
  wixData.query("Autres_Escapes")
 // Limite maximale d'items
    .limit(1000)       
      .ascending('Organisme')
      .find()
      .then(results => {
 // Appelle la function qui crée une liste unique
 const uniqueOrga = getUniqueOrga(results.items);   
 // Apelle la function qui construit la liste DropDown
        $w("#iOrganisme").options =buildOptions(uniqueOrga);
      });
 
 // Builds an array from the "Title" field only from each item in 
 // the collection and then removes the duplicates
 function getUniqueOrga(items) {    
 // Use the map method to create the titlesOnly object containing all the titles from the query results
 const titlesOnly = items.map(item => item.organisme);   
 // Return an array with a list of unique titles

 return [...new Set(titlesOnly)];
  }

// Creates an array of objects in the form {label: "label", value: "value"} from the array of titles
 function buildOptions(uniqueList) {   
 return uniqueList.map(curr => {
 // Use the map method to build the options list in the format {label:uniqueTitle, value:uniqueTitle}
 
//Début TEST Récupération du nom avec l'ID
wixData.query("Autres_Escapes_Organisme") // get the item from the database collection.
  .eq("_id", curr)
  .ascending('Organisme')
  .find()
  .then((res) => {
 const saveit = res.items[0];
    title = saveit.title
    console.log(title);//Affiche le résultat dans la console
    console.log(curr);//Affiche l'ID de la valeur
    });
// Fin test

 return {label: title, value: curr};

      });
 
  }
});

export function iOrganisme_change() {
  filter($w('#iOrganisme').value, lastFilterLieu);
}

export function iLieu_change() {
  filter(lastFilterOrganisme, $w('#iLieu').value);
  }

function filter(organisme, lieu){
 if (lastFilterOrganisme !== organisme || lastFilterLieu !== lieu) {
 let newFilter = wixData.filter();
 if (organisme)
      newFilter = newFilter.eq('organisme', organisme);
 if (lieu)
      newFilter = newFilter.eq('lieu', lieu);
    $w('#dataset1').setFilter(newFilter);   
    lastFilterOrganisme = organisme; 
    lastFilterLieu = lieu;
  }
}

Nobody??

I’m having a little trouble following your code, but one thing I noticed is that you have an incorrect sort in a couple of places in your code:

Not this:
.ascending(‘Organisme’)
but this:
.ascending(‘organisme’)

Note that field keys are lower case.

See if that helps. If you continue to have problems, please post the editor URL of your site. Only authorized Wix personnel can get access to your site in the editor.

Thanks for your response.

I’m beginner with code and take some parts from differents places. I know it isn’t perfect.

Thanks for the lower case of the fields reference.

The problem is with the return at the end of the buildOptions(uniqueList) function. When I but curr for the label, I have the ID of the reference. I try to have the title of this ID. But when I put title, I see nothing in the Dropdown.

I don’t know how I can post my editor URL. It’s the URL when I’m on the editor? How you can enter? I need to do something with autorisations?

@fabricemontandon Yes, URL when in the editor. No authorizations needed - but don’t worry, only authorized Wix people will be able to open your site in the editor.

Thanks

Your buildOptions() routine should be:

function buildOptions(uniqueList) {
    return uniqueList.map(curr => {
        return { label: curr, value: curr };
    });
}

@yisrael-wix Yes, but if I put curr for the label I have the ID in the Dropdown and I would want to see the Title of the organisme.

Can we have two different things for label and value in the return{}?

@fabricemontandon Yes, you can supply whatever you want for label and value, but you will have to add the additional information to the array that you are passing to the map. See the map() docs for more information on use. Instead of an array of strings (curr), you will need to pass an array of objects that contains the title and the value.