{"record":{"id":"729cdaeeb362e635","repo":"dotnet/AspNetCore.Docs","slug":"when-calling-ko-update-the-key-key-was-not","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/2.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/2.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/knockout.mapping.js#L641-L677","documentation":"knockout.mapping's internal getItemByKey walks the array comparing mapKey(item, callback) to the requested key; if no element matches it throws. This fires during update operations when the new data references a key that isn't currently in the mapped array — typically because the client's data is stale or the key callback is inconsistent.","triggerScenarios":"Calling a ko.update*/mappedUpdate path with data whose key is absent from the existing array; key callback returning different values for the same logical item before vs after; concurrent server edits removing the item.","commonSituations":"Stale client view-model; partial refresh that omits an item another part references; key extracted from different fields over time.","solutions":["Refresh the underlying data fully so keys line up before updating.","Verify the `key` callback reads a stable, unique field.","Handle the missing case gracefully by reloading from server rather than updating in place."],"exampleFix":"// before\n// partial update referencing a key not present -> throws\nko.mapping.fromJS(partialData, viewModel);\n// after - full refresh first\nko.mapping.fromJS(fullData, viewModel);","handlingStrategy":"validation","validationCode":"// Verify a key exists before any update path that looks it up.\nfunction keyExists(arr, key, keyCallback) {\n  return ko.utils.unwrapObservable(arr).some(function (item) {\n    return keyCallback(item) === key;\n  });\n}\nif (!keyExists(viewModel, targetKey, keyCb)) {\n  console.warn('Key not present; refreshing from server instead of updating');\n  return refresh();\n}","typeGuard":"function itemHasKey(arr, key, keyCallback) {\n  return ko.utils.unwrapObservable(arr).some(function (i) { return keyCallback(i) === key; });\n}","tryCatchPattern":"try {\n  ko.mapping.fromJS(delta, viewModel);\n} catch (e) {\n  if (/key .* was not found/.test(e.message)) {\n    console.warn('Stale view-model; doing full reload');\n    return reloadAll();\n  }\n  throw e;\n}","preventionTips":["Refresh the full collection before partial updates when keys may have drifted.","Keep the `key` callback deterministic and stable across runs.","Reconcile server/client data before invoking update paths."],"tags":["knockout","knockout-mapping","missing-key","data-sync"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}