When receiving sorted data from the server through an infinite scroll, my goal is to filter out duplicate entries and add new unique data to a client-side array. If duplicates are found, I want to stop the infinite scrolling process.
$scope.collections = [];
$scope.isBusy = false;
$scope.queryObject = {
size: 12,
sorter: 'timestamp',
sort: {
timestamp: -1
},
skip: 0
}
$scope.loadMore = function() {
if ($scope.isBusy == true) {
return;
}
$scope.isBusy = true;
Collect.all($scope.queryObject).then(function(res) {
for (var i = 0; i < res.data.length; i++) {
if ($scope.collections.length && res.data) {
for (var j = res.data.length - 1; j >= 0; j--) {
if ($scope.collections[0]._id == res.data[j]._id) {
console.log('match', res.data[j])
return;
}
}
$scope.collections.push(res.data[i])
}
else{
$scope.collections.push(res.data[i])
}
}
$scope.queryObject.skip += 12;
$scope.isBusy = false;
});
};
My approach involves checking the elements in reverse order of res.data against the first element in the collections array. However, I continue to encounter "res.data._id is not defined" error. Strangely, when removing this part and comparing collections[0] with res.data[j], it detects a match immediately. The console.log also displays res.data[j] with a defined ._id.
I suspect there might be a simple oversight causing this issue.