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

  1. Pass the JS object explicitly: ko.mapping.fromJS(data).
  2. Guard the caller: if (data) ko.mapping.fromJS(data, viewModel);
  3. 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

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


AI-assisted analysis of dotnet/AspNetCore.Docs@c67a80103a (2026-08-13). Data as JSON: /api/errors/341b7c2006940594. Report an issue: GitHub.