Repeater, Databases, & Duplicates

Edit: I was using the wrong code for a solution. Go to the last post of the page for the answer.

use a dynamic page with a repeater to show a database. The database is filled by users and their can be the same item with different information but each is given a code/number to say what type of item it is.

For the repeater I want to remove any duplicates, items with the same code/number.

I’ve tried the following code but I guess its incorrect:

import wixData from ‘wix-data’;

export function dynamicDataset_ready() {
// Run a query that returns all the items in the collection
wixData.query(“NewCollectionName2”)
// Get the max possible results from the query
.limit(1000)
.find()
.then(results => {
// Call the function that creates a list of unique titles
const uniqueTitles = getUniqueTitles(results.Index);
// Call the function that builds the options list from the unique titles
$w(“#repeater1”).data = buildOptions(uniqueTitles);
});
// Builds an array from the “Title” field only from each item in
// the collection and then removes the duplicates
function getUniqueTitles(items) {
// Use the map method to create the titlesOnly object containing all the titles from the query results
const titlesOnly = items.map(item => item.items);
// 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}
return {label:curr, value:curr};
});
}
}

Database (sorry i don’t know how to remove the white space):


BTW, do you have a link to the forum codes so I can put my code in a “code” window?

Seems your code has two errors:

It should be:

const uniqueTitles = getUniqueTitles(results.items);   

and

const titlesOnly = items.map(item => item.name); //make sure you use the right field name 

Please see the api documentation for future reference.

Sorry it didn’t work but thank you very much for helping. I did use the API documentation but couldn’t find the answer. Spent about 4-5 hours reading it

The Developer Console says: Wix code SDK error: Each item in the items array must have a member named _id which contains a unique value identifying the item.

import wixData from ‘wix-data’;

export function dynamicDataset_ready() {

wixData.query("NewCollectionName2") 
	.limit(1000)        
	.find() 
	.then(results => { 
	  	const titlesOnly = getUniqueTitles(results.items);    
	  	$w("#repeater1").data = buildOptions(titlesOnly);     
	}); 
	
function getUniqueTitles(items) {     
	const titlesOnly = items.map(item => item.Index);    
	return [...new Set(titlesOnly)]; 
} 

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

}

then try adding an _id to the options object you’re building:

return {_id: curr, label:curr, value:curr};  

Nay, that didn’t work.

Perhaps a different approach is needed to solve this; my understanding of how this works.

So on the dynamic page theirs: a dataset connected to the collection, a repeater with its elements connected to the dataset, and when the webpage is run all the items in the collection show up on the webpage.

The idea is to remove duplicates from the repeater. The collection has a field/column with the name Index. Index is just some random numbers like 1, 1, 2, 3, 3. And so the code below should go through the collections Index and remove any duplicate entries.

//Runs after the dataset has loaded.
//Perhaps I should run this after the repeater is loaded too using onReady?
export function dynamicDataset_ready() {
//Collects all the table data from the collection.
wixData.query(“NewCollectionName2”)
.limit(1000)
.find()
.then(results => {
//Runs for each item/line in the collection table.
const titlesOnly = getUniqueTitles(results.items);
//Tells the repeater to update its data/information based on the new collection.
$w(“#repeater1”).data = buildOptions(titlesOnly);
});

//Rewrites the table data from the collection by overwriting duplicate entries. 
//For instance, if two items in the table use the number 1 for Index then the second one overwrites the first. 
function getUniqueTitles(items) {     
	const titlesOnly = items.map(items => items.Index); //Overwrites 
	return [...new Set(titlesOnly)]; //Returns the new collection with the overwrites. 
} 

//I don't know how this works. Can i get a link to the API for how .map works? 
function buildOptions(uniqueList) {    
	return uniqueList.map(curr => { 
		return {_id:curr, label:curr, value:curr};   
	}); 
} 

}

did you find a solution? i am looking for the same thing

Not as of yet and my understanding of the code was incorrect.

1 Like

Here’s my current fix that works but their is one problem, when finding duplicates it removes the first entry instead of the second.

import wixData from ‘wix-data’;

export function dynamicDataset_ready() {
wixData.query(“NewCollectionName2”).limit(1000).find().then(results =>
$w(“#repeater1”).data = Array.from(results.items.reduce((m, t) => m.set(t.title, t), new Map()).values()); });

Update, the below code works now.
This changes the dataset you have added to your webpage.

Change “NewCollectionName2” to the name of your collection.
Change “repeater1” to the name of your repeater.
Change “t.title” to t..
*Note for the above “t.title”: Your collection has a hidden ‘field key’ which is used instead of the field name you gave it, for use with java code. Go to your collection, find your field/name, left-click the three dots, select 'manage properties, look for ‘field key’, and use that instead of your fields name.

import wixData from 'wix-data';

export function dynamicDataset_ready() {
		wixData.query("NewCollectionName2").limit(1000).find().then(results => {
		$w("#repeater1").data = Array.from(results.items.reduceRight((m, t) => m.set(t.title, t), new Map()).values()).reverse();
  	});
}

Any questions?

2 Likes