dianping/cat · error · TypeError

can't convert {o} to object

Error message

can't convert {o} to object

What it means

An internal toObject helper in the bundled es5-shim: it coerces an argument to an object and throws this TypeError when the value is null or undefined, since Object(null) itself would not throw but the operation calling toObject (e.g. Function.prototype.apply/bind argument processing) requires an actual object. The message embeds the value ('null' or 'undefined' as text).

Source

Thrown at cat-home/src/main/webapp/assets/js/editor/worker-xquery.js:49531

    valueOf = input.valueOf;
    if (typeof valueOf === "function") {
        val = valueOf.call(input);
        if (isPrimitive(val)) {
            return val;
        }
    }
    toString = input.toString;
    if (typeof toString === "function") {
        val = toString.call(input);
        if (isPrimitive(val)) {
            return val;
        }
    }
    throw new TypeError();
}
var toObject = function (o) {
    if (o == null) { // this matches both null and undefined
        throw new TypeError("can't convert "+o+" to object");
    }
    return Object(o);
};

});

View on GitHub (pinned to e815e74d4c)

Solutions

  1. Guard before the call: if (list == null) list = [];
  2. Fix the upstream source of null/undefined (the missing payload or uninitialized variable).
  3. Prefer Array.from/list.slice patterns on values you have already defaulted.

Example fix

// before
Array.prototype.forEach.call(items, render); // items may be undefined

// after
(items || []).forEach(render);
Defensive patterns

Strategy: validation

Validate before calling

if (list == null) list = []; // or throw a contextual error naming the missing field

Type guard

function isNonNullObject(v) {
  return v !== null && v !== undefined && typeof v === 'object';
}

Prevention

When it happens

Trigger: Passing null/undefined where the shim internally calls toObject — most commonly Function.prototype.apply(null-ish thisArg lists) polyfills, or Array.prototype slice/concat-style helpers invoked on null. Also hit when caller code does Array.prototype.forEach.call(null, fn) under the shim.

Common situations: Calling array generics on a possibly-null collection; a callback receiving undefined because an event or XHR delivered no payload; refactoring that left a variable uninitialized in one branch.

Related errors


AI-assisted analysis of dianping/cat@e815e74d4c (2026-08-14). Data as JSON: /api/errors/6f2e1a2b34a16e4b. Report an issue: GitHub.