dotnet/AspNetCore.Docs · error · Error
When calling ko.fromJS, pass the object you want to convert.
Error message
When calling ko.fromJS, pass the object you want to convert.
What it means
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.
Source
Thrown at aspnetcore/mvc/controllers/testing/samples/2.x/TestingControllersSample/src/TestingControllersSample/wwwroot/js/knockout.mapping.js:80
}
}
}
function merge(obj1, obj2) {
var merged = {};
extendObject(merged, obj1);
extendObject(merged, obj2);
return merged;
}
exports.isMapped = function (viewModel) {
var unwrapped = ko.utils.unwrapObservable(viewModel);
return unwrapped && unwrapped[mappingProperty];
}
exports.fromJS = function (jsObject /*, inputOptions, target*/ ) {
if (arguments.length == 0) throw new Error("When calling ko.fromJS, pass the object you want to convert.");
try {
if (!mappingNesting++) {
dependentObservables = [];
visitedObjects = new objectLookup();
}
var options;
var target;
if (arguments.length == 2) {
if (arguments[1][mappingProperty]) {
target = arguments[1];
} else {
options = arguments[1];
}
}
if (arguments.length == 3) {View on GitHub (pinned to c67a80103a)
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 || {}).
Example fix
// before
ko.mapping.fromJS(); // throws
// after
ko.mapping.fromJS(data || {}); Defensive patterns
Strategy: validation
Validate before calling
// Guard fromJS against missing/undefined data.
function mapFromJS(data, options, target) {
if (arguments.length === 0 || data == null) {
throw new TypeError('ko.mapping.fromJS requires a non-null data object');
}
return target !== undefined
? ko.mapping.fromJS(data, options || {}, target)
: ko.mapping.fromJS(data, options || {});
} Type guard
function isMappableObject(x) {
return x != null && typeof x === 'object' && !ko.isObservable(x);
} Try / catch
try {
ko.mapping.fromJS(data, viewModel);
} catch (e) {
if (/pass the object you want to convert/.test(e.message)) {
console.error('fromJS called with no data; skipping mapping', data);
} else { throw e; }
} Prevention
- 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.
When it happens
Trigger: 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)).
Common situations: 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.
Related errors
- When calling ko.mapping.toJS, pass the object you want to co
- When calling ko.fromJS, pass the object you want to convert.
- ko.mapping.defaultOptions().ignore should be an array.
- ko.mapping.defaultOptions().include should be an array.
- ko.mapping.updateFromJS, use ko.mapping.fromJS instead. Plea
AI-assisted analysis of dotnet/AspNetCore.Docs@c67a80103a (2026-08-13).
Data as JSON: /api/errors/341b7c2006940594.
Report an issue: GitHub.