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
Identical guard to error 4 in the 3.x sample's knockout.mapping.js: fromJS requires at least one argument (the JS object to convert) and throws on zero arguments.
Source
Thrown at aspnetcore/mvc/controllers/testing/samples/3.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 object: ko.mapping.fromJS(data).
- Guard: if (data) ko.mapping.fromJS(data);
- Fallback: ko.mapping.fromJS(data || {}).
Example fix
// before
ko.mapping.fromJS(); // throws
// after
ko.mapping.fromJS(data || {}); Defensive patterns
Strategy: validation
Validate before calling
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', data);
} else { throw e; }
} Prevention
- Coalesce missing payloads: ko.mapping.fromJS(data || {}).
- Validate AJAX responses are objects before mapping.
- Wrap mapping in a helper enforcing a non-null argument.
When it happens
Trigger: Calling ko.mapping.fromJS() with no arguments in the 3.x sample; passing through an undefined data variable.
Common situations: AJAX returning null; refactor dropping the parameter; conditional data flow.
Related errors
- When calling ko.fromJS, pass the object you want to convert.
- When calling ko.mapping.toJS, pass the object you want to co
- 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/649913d970382496.
Report an issue: GitHub.