{"record":{"id":"341b7c2006940594","repo":"dotnet/AspNetCore.Docs","slug":"when-calling-ko-fromjs-pass-the-object-you-want-t","errorCode":null,"errorMessage":"When calling ko.fromJS, pass the object you want to convert.","messagePattern":"When calling ko\\.fromJS, pass the object you want to convert\\.","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":80,"sourceCode":"\t\t\t}\n\t\t}\n\t}\n\n\tfunction merge(obj1, obj2) {\n\t\tvar merged = {};\n\t\textendObject(merged, obj1);\n\t\textendObject(merged, obj2);\n\n\t\treturn merged;\n\t}\n\n\texports.isMapped = function (viewModel) {\n\t\tvar unwrapped = ko.utils.unwrapObservable(viewModel);\n\t\treturn unwrapped && unwrapped[mappingProperty];\n\t}\n\n\texports.fromJS = function (jsObject /*, inputOptions, target*/ ) {\n\t\tif (arguments.length == 0) throw new Error(\"When calling ko.fromJS, pass the object you want to convert.\");\n\n\t\ttry {\n\t\t\tif (!mappingNesting++) {\n\t\t\t\tdependentObservables = [];\n\t\t\t\tvisitedObjects = new objectLookup();\n\t\t\t}\n\n\t\t\tvar options;\n\t\t\tvar target;\n\n\t\t\tif (arguments.length == 2) {\n\t\t\t\tif (arguments[1][mappingProperty]) {\n\t\t\t\t\ttarget = arguments[1];\n\t\t\t\t} else {\n\t\t\t\t\toptions = arguments[1];\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (arguments.length == 3) {","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/mvc/controllers/testing/samples/2.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/knockout.mapping.js#L62-L98","documentation":"knockout.mapping's fromJS converts a plain JS object into an observable view model. Its first line is a guard: zero arguments means nothing to map, so it throws rather than silently returning undefined. The library treats this as a programmer error, not a data condition.","triggerScenarios":"Calling ko.mapping.fromJS() with no arguments; calling it with arguments.length === 0 because a data variable was undefined and spread incorrectly (ko.mapping.fromJS(...undefined)).","commonSituations":"AJAX callback returning null/undefined and the value being passed straight in; refactor that dropped the data parameter; conditional data flow that occasionally omits the arg.","solutions":["Pass the JS object explicitly: ko.mapping.fromJS(data).","Guard the caller: if (data) ko.mapping.fromJS(data, viewModel);","Default to an empty object when data is missing: ko.mapping.fromJS(data || {})."],"exampleFix":"// before\nko.mapping.fromJS(); // throws\n// after\nko.mapping.fromJS(data || {});","handlingStrategy":"validation","validationCode":"// Guard fromJS against missing/undefined data.\nfunction mapFromJS(data, options, target) {\n  if (arguments.length === 0 || data == null) {\n    throw new TypeError('ko.mapping.fromJS requires a non-null data object');\n  }\n  return target !== undefined\n    ? ko.mapping.fromJS(data, options || {}, target)\n    : ko.mapping.fromJS(data, options || {});\n}","typeGuard":"function isMappableObject(x) {\n  return x != null && typeof x === 'object' && !ko.isObservable(x);\n}","tryCatchPattern":"try {\n  ko.mapping.fromJS(data, viewModel);\n} catch (e) {\n  if (/pass the object you want to convert/.test(e.message)) {\n    console.error('fromJS called with no data; skipping mapping', data);\n  } else { throw e; }\n}","preventionTips":["Coalesce missing payloads: ko.mapping.fromJS(data || {}).","Validate AJAX responses are objects before mapping.","Wrap mapping calls in a helper that enforces a non-null argument."],"tags":["knockout","knockout-mapping","validation","arguments"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}