const TYPES = await get_UniqueData(userItems, "type"); console.log("All-Types: ", TYPES);
OK! At first i want to recommend you the following. If you are working with code → you perhaps should think about to use pure code and without mixing DATASETs + CODE.
What do i mean? If i would be you → i would generate absolutely EVERYTHING by code, without using DATASET-CONNECTIONS. You can use DATASETS, but generate connections and cummunications between DATASET and PAGE by → CODE, instead of using the option in the properties panel. This will save you headaches in some situations.
A simple example: Populating DROPDOWN by code…
You want to populate your dropdown from a DATA-FIELD of your wished DATABASE?
But you do not know how to do it → because your DATA-FIELD includes also a lot of DUBLICATES → but you want just to see UNIQUE-ENTRIES? And all this done by → CODE?
Take a look onto this example…
All you will need to do in this example, is to set-up 3-VALUES:
- Your ITEM-DATA-QUERY: the ITEM-RESULTS of your DATABASE-QUERY
- Selecting the right DATA-FIELD: for example → “title” which is always given inside DB.
- The ID of your DropDown you want to populate.
create_UniqueDropdown(itemData, "title", "#DropDownIDhere");
function create_UniqueDropdown(items, dbfield, dropdown) {
const uniqueTitles = getUniqueTitles(items); $w(dropdown).options = buildOptions(uniqueTitles);
function getUniqueTitles(items) {const titlesOnly = items.map(item => item[dbfield]); return [...new Set(titlesOnly)];}
function buildOptions(uniqueList) {return uniqueList.map(curr => {return {label:curr, value:curr};});}
}
OK! Let’s do it together!
Your DATABASE-ID is? → “committeessetup”
const DATABASE = "committeessetup"
The field you need is? → “title”
const DB_FIELD = "title"
Your query:
const QUERY = wixData.query(DATABASE)
Your DropDown-ID:
const ddCommitee = "#dropdown2"
All the process starts when you select an option from dropdown (DropDown.onChange)
$w(ddCommitee).onChange(()=>{
});
Of course everything inside → onReady()
$w.onReady(()=>{ ... all here inside ... });
import wixData from'wix-data';
const itemData;
//----------- USER-INTERFACE ------------------
const DB_FIELD = "title";
const DATABASE = "committeessetup"
const ddCommitee = "#dropdown2"
//----------- USER-INTERFACE ------------------
$w.onReady(()=>{
$w(ddCommitee).onChange(()=>{
create_UniqueDropdown(itemData, DB_FIELD, ddCommitee);
});
});
function create_UniqueDropdown(items, dbfield, dropdown) {
const uniqueTitles = getUniqueTitles(items); $w(dropdown).options = buildOptions(uniqueTitles);
function getUniqueTitles(items) {const titlesOnly = items.map(item => item[dbfield]); return [...new Set(titlesOnly)];}
function buildOptions(uniqueList) {return uniqueList.map(curr => {return {label:curr, value:curr};});}
}
OK, till here everything ok and clear? Of course not !!! What do you still missing?
Yes → You are still missing the DATA-Query (itemData), to be able to populate your dropdown.
Lets do first the DATA-QUERY:
let QUERY = wixData.query("committeessetup");
QUERY.find()
.then((res)=> {console.log(res);
if (res.items.length > 0) {let itemData = res.items;}
else {console.log("No DB-Entries found!");}
}).catch((err)=>{console.log(err);});
After TRANSFORMATION it into a → RETURNING-FUNCTION…
function get_ItemData() {
let QUERY = wixData.query("committeessetup");
QUERY.find()
.then((res)=> {console.log(res);
if (res.items.length > 0) {let itemData = res.items; return itemData}
else {console.log("No DB-Entries found!");}
}).catch((err)=>{console.log(err);});
}
The COMPLETE-CODE: for creation of an unique DropDown by CODE automaticaly out of your own DATABASE (without any DUBLICATES).
import wixData from'wix-data';
//----------- USER-INTERFACE ------------------
const DB_FIELD = "title";
const DATABASE = "committeessetup"
const ddCommitee = "#dropdown2"
//----------- USER-INTERFACE ------------------
$w.onReady(async()=>{
//First getting QUERY-DATA (DATABASE-DATA)....getting our itemData...
let itemData = await get_ItemData(); console.log(itemData);
//now you have all your DATABASE-DATA inside --> "itemData".
$w(ddCommitee).onChange(()=>{//this will start the --> creation of a unique DropDown....
create_UniqueDropdown(itemData, DB_FIELD, ddCommitee);
});
});
function get_ItemData() {
let QUERY = wixData.query("committeessetup");
QUERY.find()
.then((res)=> {console.log(res);
if (res.items.length > 0) {let itemData = res.items; return itemData}
else {console.log("No DB-Entries found!");}
}).catch((err)=>{console.log(err);});
}
function create_UniqueDropdown(items, dbfield, dropdown) {
const uniqueTitles = getUniqueTitles(items); $w(dropdown).options = buildOptions(uniqueTitles);
function getUniqueTitles(items) {const titlesOnly = items.map(item => item[dbfield]); return [...new Set(titlesOnly)];}
function buildOptions(uniqueList) {return uniqueList.map(curr => {return {label:curr, value:curr};});}
}
I would say → FIRST-STEP done?
Let me know if it worked 
Question: What is the speciality of this code? What’s it’s magic?