dotnet/AspNetCore.Docs · error · Error
There already is an object with the key that you specified.
Error message
There already is an object with the key that you specified.
What it means
knockout.mapping.js's `mappedCreate` checks `mappedIndexOf(value) !== -1` and throws if the item's key already exists in the mapped observable array. Each mapped array is keyed by a callback (often an id property), and uniqueness is enforced on create.
Source
Thrown at aspnetcore/mvc/controllers/testing/samples/3.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/knockout.mapping.js:542
var arrayOfKeys = filterArrayByKey(arrayOfValues, keyCallback);
return mappedRootObject.destroy(function (item) {
return ko.utils.arrayIndexOf(arrayOfKeys, keyCallback(item)) != -1;
});
}
mappedRootObject.mappedIndexOf = function (item) {
var keys = filterArrayByKey(mappedRootObject(), keyCallback);
var key = keyCallback(item);
return ko.utils.arrayIndexOf(keys, key);
}
mappedRootObject.mappedGet = function (item) {
return mappedRootObject()[mappedRootObject.mappedIndexOf(item)];
}
mappedRootObject.mappedCreate = function (value) {
if (mappedRootObject.mappedIndexOf(value) !== -1) {
throw new Error("There already is an object with the key that you specified.");
}
var item = hasCreateCallback() ? createCallback(value) : value;
if (hasUpdateCallback()) {
var newValue = updateCallback(item, value);
if (ko.isWriteableObservable(item)) {
item(newValue);
} else {
item = newValue;
}
}
mappedRootObject.push(item);
return item;
}
}
var currentArrayKeys = filterArrayByKey(ko.utils.unwrapObservable(mappedRootObject), keyCallback).sort();
var newArrayKeys = filterArrayByKey(rootObject, keyCallback);View on GitHub (pinned to c67a80103a)
Solutions
- Before creating, check `if (myMappedArray.mappedIndexOf(newItem) === -1) myMappedArray.mappedCreate(newItem);`.
- Use `ko.mapping.fromJS(newData, { key: x => x.id }, myMappedArray)` which updates existing keys in place rather than creating duplicates.
- Dedupe the incoming dataset by the key property before mapping.
- Verify the key callback returns a genuinely unique value per item.
Example fix
// before
myMappedArray.mappedCreate(newItem);
// after
if (myMappedArray.mappedIndexOf(newItem) === -1) {
myMappedArray.mappedCreate(newItem);
} Defensive patterns
Strategy: validation
Validate before calling
function safeCreate(mappedArray, value) {
if (mappedArray.mappedIndexOf(value) !== -1) {
throw new Error('Duplicate key — use fromJS with a key to update instead');
}
mappedArray.mappedCreate(value);
} Type guard
function isUniqueKey(mappedArray, value) {
return mappedArray.mappedIndexOf(value) === -1;
} Try / catch
try {
myArray.mappedCreate(item);
} catch (e) {
if (/already is an object/.test(e.message)) {
ko.mapping.fromJS([item], { key: x => x.id }, myArray);
} else throw e;
} Prevention
- Dedupe server data by key before mapping.
- Prefer fromJS with a key callback over mappedCreate for upsert semantics.
- Verify the key callback returns unique values.
When it happens
Trigger: Calling `myMappedArray.mappedCreate(newItem)` where `mapKey(newItem)` matches an existing element's key. Also triggered by importing data with duplicate ids into a keyed mapping.
Common situations: Server data containing duplicate primary keys; re-running fromJS over an array that already has the items without a key update path; wrong key callback that collapses distinct items to the same key.
Related errors
- When calling ko.mapping.toJS, pass the object you want to co
- When calling ko.update*, the key '${key}' was not found!
- When calling ko.fromJS, pass the object you want to convert.
- When calling ko.mapping.toJS, pass the object you want to co
- ko.mapping.defaultOptions().ignore should be an array.
AI-assisted analysis of dotnet/AspNetCore.Docs@c67a80103a (2026-08-13).
Data as JSON: /api/errors/0fd350e0595ea6ed.
Report an issue: GitHub.