{"record":{"id":"20449a49068a5dda","repo":"dotnet/AspNetCore.Docs","slug":"when-calling-ko-mapping-tojs-pass-the-object-you-20449a","errorCode":null,"errorMessage":"When calling ko.mapping.toJS, pass the object you want to convert.","messagePattern":"When calling ko\\.mapping\\.toJS, pass the object you want to convert\\.","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":154,"sourceCode":"\n\texports.fromJSON = function (jsonString /*, options, target*/ ) {\n\t\tvar parsed = ko.utils.parseJson(jsonString);\n\t\targuments[0] = parsed;\n\t\treturn exports.fromJS.apply(this, arguments);\n\t};\n\n\texports.updateFromJS = function (viewModel) {\n\t\tthrow new Error(\"ko.mapping.updateFromJS, use ko.mapping.fromJS instead. Please note that the order of parameters is different!\");\n\t};\n\n\texports.updateFromJSON = function (viewModel) {\n\t\tthrow new Error(\"ko.mapping.updateFromJSON, use ko.mapping.fromJSON instead. Please note that the order of parameters is different!\");\n\t};\n\n\texports.toJS = function (rootObject, options) {\n\t\tif (!defaultOptions) exports.resetDefaultOptions();\n\n\t\tif (arguments.length == 0) throw new Error(\"When calling ko.mapping.toJS, pass the object you want to convert.\");\n\t\tif (exports.getType(defaultOptions.ignore) !== \"array\") throw new Error(\"ko.mapping.defaultOptions().ignore should be an array.\");\n\t\tif (exports.getType(defaultOptions.include) !== \"array\") throw new Error(\"ko.mapping.defaultOptions().include should be an array.\");\n\t\tif (exports.getType(defaultOptions.copy) !== \"array\") throw new Error(\"ko.mapping.defaultOptions().copy should be an array.\");\n\n\t\t// Merge in the options used in fromJS\n\t\toptions = fillOptions(options, rootObject[mappingProperty]);\n\n\t\t// We just unwrap everything at every level in the object graph\n\t\treturn exports.visitModel(rootObject, function (x) {\n\t\t\treturn ko.utils.unwrapObservable(x)\n\t\t}, options);\n\t};\n\n\texports.toJSON = function (rootObject, options) {\n\t\tvar plainJavaScriptObject = exports.toJS(rootObject, options);\n\t\treturn ko.utils.stringifyJson(plainJavaScriptObject);\n\t};\n","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/mvc/controllers/testing/samples/3.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/knockout.mapping.js#L136-L172","documentation":"knockout.mapping.js guards `toJS` by checking `arguments.length == 0` and throws when no rootObject is passed. toJS recursively unwraps observables, so it has nothing to operate on without a target object.","triggerScenarios":"Calling `ko.mapping.toJS()` with zero arguments, or `ko.mapping.toJS(undefined)` where the argument is omitted entirely. Note: passing `undefined` explicitly still increments arguments.length in non-strict mode, so the real trigger is omitting the parameter.","commonSituations":"A variable intended as rootObject evaluated to nothing and the call was made unconditionally; refactored code that dropped the argument; calling toJS on a knockout object before it was assigned.","solutions":["Pass the mapped view model: `ko.mapping.toJS(myViewModel)`.","Guard the call so it only runs when the source object exists: `if (myViewModel) ko.mapping.toJS(myViewModel)`.","If serializing to JSON, prefer `ko.mapping.toJSON(rootObject)` which wraps toJS."],"exampleFix":"// before\nvar plain = ko.mapping.toJS();\n\n// after\nvar plain = ko.mapping.toJS(myViewModel);","handlingStrategy":"validation","validationCode":"function safeToJS(rootObject, options) {\n  if (arguments.length === 0 || rootObject == null) {\n    throw new TypeError('ko.mapping.toJS requires a root object');\n  }\n  return ko.mapping.toJS(rootObject, options);\n}","typeGuard":"function isMappable(obj) {\n  return obj != null && typeof obj === 'object';\n}","tryCatchPattern":"try {\n  const plain = ko.mapping.toJS(myViewModel);\n} catch (e) {\n  if (/pass the object/.test(e.message)) console.error('Missing rootObject for toJS');\n  throw e;\n}","preventionTips":["Always pass the root view model explicitly.","Null-check before calling: `if (myViewModel) ko.mapping.toJS(myViewModel)`.","Wrap toJS in a project helper that enforces the argument."],"tags":["knockout","validation","argument-error","mapping"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}