Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 1812af5

Browse files
smillibtford
authored andcommitted
fix(ngRepeat): improve errors for duplicate items
-Log the value that had the duplicate key, as well as the key The error that is thrown when items have duplicate track by keys can be confusing because only the duplicate key is logged. If the user didn't provide that key themselves, they may not know what it is or what item it corresponds to.
1 parent 19cb2e3 commit 1812af5

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/ng/directive/ngRepeat.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,9 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
309309
if (block && block.scope) lastBlockMap[block.id] = block;
310310
});
311311
// This is a duplicate and we need to throw an error
312-
throw ngRepeatMinErr('dupes', "Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: {0}, Duplicate key: {1}",
313-
expression, trackById);
312+
throw ngRepeatMinErr('dupes',
313+
"Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: {0}, Duplicate key: {1}, Duplicate value: {2}",
314+
expression, trackById, toJson(value));
314315
} else {
315316
// new never before seen block
316317
nextBlockOrder[index] = { id: trackById };

test/ng/directive/ngRepeatSpec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,8 @@ describe('ngRepeat', function() {
940940
scope.items = [a, a, a];
941941
scope.$digest();
942942
expect($exceptionHandler.errors.shift().message).
943-
toMatch(/^\[ngRepeat:dupes\] Duplicates in a repeater are not allowed\. Use 'track by' expression to specify unique keys\. Repeater: item in items, Duplicate key: object:003/);
943+
toMatch(
944+
/^\[ngRepeat:dupes\] Duplicates in a repeater are not allowed\. Use 'track by' expression to specify unique keys\. Repeater: item in items, Duplicate key: object:003, Duplicate value: {}/);
944945

945946
// recover
946947
scope.items = [a];
@@ -960,7 +961,8 @@ describe('ngRepeat', function() {
960961
scope.items = [d, d, d];
961962
scope.$digest();
962963
expect($exceptionHandler.errors.shift().message).
963-
toMatch(/^\[ngRepeat:dupes\] Duplicates in a repeater are not allowed\. Use 'track by' expression to specify unique keys\. Repeater: item in items, Duplicate key: object:009/);
964+
toMatch(
965+
/^\[ngRepeat:dupes\] Duplicates in a repeater are not allowed\. Use 'track by' expression to specify unique keys\. Repeater: item in items, Duplicate key: object:009, Duplicate value: {}/);
964966

965967
// recover
966968
scope.items = [a];

0 commit comments

Comments
 (0)