{"record":{"id":"58da71766f3d4174","repo":"dotnet/AspNetCore.Docs","slug":"when-calling-ko-update-the-key-key-was-not-58da71","errorCode":null,"errorMessage":"When calling ko.update*, the key '${key}' was not found!","messagePattern":"When calling ko\\.update\\*, the key '(.+?)' was not found!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"aspnetcore/mvc/controllers/testing/samples/3.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/knockout.mapping.js","lineNumber":659,"sourceCode":"\t\treturn null;\n\t}\n\n\tfunction mapKey(item, callback) {\n\t\tvar mappedItem;\n\t\tif (callback) mappedItem = callback(item);\n\t\tif (exports.getType(mappedItem) === \"undefined\") mappedItem = item;\n\n\t\treturn ko.utils.unwrapObservable(mappedItem);\n\t}\n\n\tfunction getItemByKey(array, key, callback) {\n\t\tarray = ko.utils.unwrapObservable(array);\n\t\tfor (var i = 0, j = array.length; i < j; i++) {\n\t\t\tvar item = array[i];\n\t\t\tif (mapKey(item, callback) === key) return item;\n\t\t}\n\n\t\tthrow new Error(\"When calling ko.update*, the key '\" + key + \"' was not found!\");\n\t}\n\n\tfunction filterArrayByKey(array, callback) {\n\t\treturn ko.utils.arrayMap(ko.utils.unwrapObservable(array), function (item) {\n\t\t\tif (callback) {\n\t\t\t\treturn mapKey(item, callback);\n\t\t\t} else {\n\t\t\t\treturn item;\n\t\t\t}\n\t\t});\n\t}\n\n\tfunction visitPropertiesOrArrayEntries(rootObject, visitorCallback) {\n\t\tif (exports.getType(rootObject) === \"array\") {\n\t\t\tfor (var i = 0; i < rootObject.length; i++)\n\t\t\tvisitorCallback(i);\n\t\t} else {\n\t\t\tfor (var propertyName in rootObject)","sourceCodeStart":641,"sourceCodeEnd":677,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/mvc/controllers/testing/samples/3.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/knockout.mapping.js#L641-L677","documentation":"knockout.mapping.js's internal `getItemByKey` iterates a mapped array and throws when no element's `mapKey` equals the requested key. It backs `ko.update*` operations that locate an item by key before updating it.","triggerScenarios":"Calling a key-based update (e.g. via the mapping plugin's keyed update path) with a key that does not match any element returned by the configured key callback.","commonSituations":"Key callback returns a different property than the caller used (id vs _id); item was removed from the array before the update; stale client-side state referencing a deleted record; case mismatch in key values.","solutions":["Confirm the key passed matches what the configured `key` callback extracts (same property name and type).","Check existence with `mappedIndexOf` before attempting the keyed update.","Refresh the mapped array from the server before updating so the target key is present.","Normalize key casing and type (string vs number) on both sides."],"exampleFix":"// before\nupdateByKey(mappedArray, '42', patch);\n\n// after\nif (mappedArray.mappedIndexOf({ id: 42 }) !== -1) {\n  updateByKey(mappedArray, '42', patch);\n}","handlingStrategy":"validation","validationCode":"function safeUpdateByKey(mappedArray, key, callback, patch) {\n  if (mappedArray.mappedIndexOf({ /* match shape */ }) === -1) {\n    throw new Error('Key not present: ' + key);\n  }\n  // proceed with keyed update\n}","typeGuard":"function keyExists(mappedArray, key, keyCallback) {\n  return filterArrayByKey(mappedArray, keyCallback).indexOf(key) !== -1;\n}","tryCatchPattern":"try {\n  updateByKey(arr, key, cb, patch);\n} catch (e) {\n  if (/was not found/.test(e.message)) {\n    refreshFromServer(); // re-sync then retry\n  } else throw e;\n}","preventionTips":["Refresh the mapped array before key-based updates.","Normalize key type/casing on both sides.","Confirm the key callback property matches the key being passed."],"tags":["knockout","key-not-found","mapping","validation"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}