Filtering gallery with tags causing duplicates

I have a site where the gallery could show up to a thousand images. Attached to each image is a series of tags, and then at the top of the page there is a tag bar so you can filter the images.

This is works. The images load, you click a tag, and the images filter. If you select multiple tags it will filter images that have all those tags. Easy.

Except for one issue…

If I select tag 1 then tag 2, then unselect tag 1, then unselect tag 2, then select tag 1 again all the images are filtered correctly as per tag 1. Except, it is now an infinite scrolling gallery where the images just duplicate over and over again. So instead of say 20 images, I ended up with thousands of duplicates that match my tag.

This is the code that is called every time a tag is clicked. Basically, all it’s doing is filtering by the value of my tag bar, and then between two number values. The issue isn’t with the number values, it’s with the tag part.

export function DoFilter ( )
$w ( “#dataset2” ). setFilter ( wixData . filter ()
. hasAll ( “tags” , $w ( “#selectionTags1” ). value )
. ge ( “number” , parseInt ( $w ( “#input4” ). value ))
. le ( “number” , parseInt ( $w ( “#input3” ). value ))). then ( done => {
$w ( “#text6” ). text = $w ( “#dataset2” ). getTotalCount () + " Items" ;

Anyone know what’s going on?