dianping/cat · error · TypeError
reduceRight of empty array with no initial value
Error message
reduceRight of empty array with no initial value
What it means
ES5-shim Array.prototype.reduceRight throws 'reduceRight of empty array with no initial value' when length is 0 and no initialValue argument was given — the right-to-left twin of reduce's rule that an accumulator must come from somewhere.
Source
Thrown at cat-home/src/main/webapp/assets/js/editor/worker-xquery.js:49147
result = fun.call(void 0, result, self[i], i, object);
}
}
return result;
};
}
if (!Array.prototype.reduceRight) {
Array.prototype.reduceRight = function reduceRight(fun /*, initial*/) {
var object = toObject(this),
self = splitString && _toString(this) == "[object String]" ?
this.split("") :
object,
length = self.length >>> 0;
if (_toString(fun) != "[object Function]") {
throw new TypeError(fun + " is not a function");
}
if (!length && arguments.length == 1) {
throw new TypeError("reduceRight of empty array with no initial value");
}
var result, i = length - 1;
if (arguments.length >= 2) {
result = arguments[1];
} else {
do {
if (i in self) {
result = self[i--];
break;
}
if (--i < 0) {
throw new TypeError("reduceRight of empty array with no initial value");
}
} while (true);
}
do {View on GitHub (pinned to e815e74d4c)
Solutions
- Always supply the initial value: arr.reduceRight(fn, init).
- Short-circuit empty inputs with a default result before calling reduceRight.
- Initialize the accumulator consistently so first-iteration behavior is deterministic.
Example fix
// before
var composed = fns.reduceRight(compose2);
// after
var composed = fns.reduceRight(compose2, function(x) { return x; }); Defensive patterns
Strategy: validation
Validate before calling
if (arr.length === 0 && arguments.length < 2) {
return identity; // or default — avoids the empty-array TypeError
}
return arr.reduceRight(fn, initialValue); Try / catch
try { return arr.reduceRight(fn); }
catch (e) {
if (e instanceof TypeError && /no initial value/.test(e.message)) {
return fallbackValue;
}
throw e;
} Prevention
- Always pass an explicit initial value to reduceRight.
- Short-circuit empty collections with a default result before folding.
When it happens
Trigger: [].reduceRight(fn); — empty array after a filter/map chain, a queue drained to zero, or data not yet arrived at reduce time.
Common situations: Reducing an empty filtered set; race where reduce runs before data loads; empty tail recursion helper arrays.
Related errors
- reduce of empty array with no initial value
- {fun} is not a function
- Object.getOwnPropertyDescriptor called on a non-object: {obj
- typeof prototype[{typeof prototype}] != 'object'
- Object.defineProperty called on non-object: {object}
AI-assisted analysis of dianping/cat@e815e74d4c (2026-08-14).
Data as JSON: /api/errors/82caf107a2678976.
Report an issue: GitHub.