{"record":{"id":"65c61f7261d830a0","repo":"dotnet/AspNetCore.Docs","slug":"ko-mapping-defaultoptions-ignore-should-be-an-ar","errorCode":null,"errorMessage":"ko.mapping.defaultOptions().ignore should be an array.","messagePattern":"ko\\.mapping\\.defaultOptions\\(\\)\\.ignore should be an array\\.","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":155,"sourceCode":"\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\n\texports.defaultOptions = function () {","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/mvc/controllers/testing/samples/2.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/knockout.mapping.js#L137-L173","documentation":"knockout.mapping lets you set global default options (ignore/include/copy arrays). toJS validates that defaultOptions().ignore is an array; if a caller set it to a string or object, mapping would misbehave, so it throws on the next toJS/fromJS call. This catches misconfiguration early.","triggerScenarios":"ko.mapping.defaultOptions().ignore = 'name'; (string) instead of ['name']; setting it to null; assigning an object.","commonSituations":"Misreading the API docs; copy-paste from an example that omitted brackets; building the ignore list dynamically and forgetting to wrap in an array.","solutions":["Set ignore as an array: ko.mapping.defaultOptions().ignore = ['name', 'id'];","When building dynamically, ensure the variable is an array before assigning.","Check the other default options (include, copy) at the same time — they are validated next."],"exampleFix":"// before\nko.mapping.defaultOptions().ignore = 'internalId';\n// after\nko.mapping.defaultOptions().ignore = ['internalId'];","handlingStrategy":"validation","validationCode":"// Enforce array shape for defaultOptions().ignore at config time.\nfunction setIgnoreList(keys) {\n  if (!Array.isArray(keys)) {\n    throw new TypeError('ignore list must be an array, got ' + typeof keys);\n  }\n  ko.mapping.defaultOptions().ignore = keys;\n}","typeGuard":"function isStringArray(x) { return Array.isArray(x) && x.every(function (k) { return typeof k === 'string'; }); }","tryCatchPattern":"try {\n  ko.mapping.toJS(viewModel);\n} catch (e) {\n  if (/ignore should be an array/.test(e.message)) {\n    console.error('Fix defaultOptions().ignore to be an array');\n    ko.mapping.defaultOptions().ignore = [];\n  } else { throw e; }\n}","preventionTips":["Always set ignore/include/copy as arrays, even single-item: ['field'] not 'field'.","Validate option lists in a config helper that wraps defaultOptions().","Audit all three list options together — they share the array requirement."],"tags":["knockout","knockout-mapping","configuration","validation"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}