{"record":{"id":"6aeb1b9a0207e4dd","repo":"meteor/meteor","slug":"reduce-of-empty-array-with-no-initial-value","errorCode":null,"errorMessage":"Reduce of empty array with no initial value","messagePattern":"Reduce of empty array with no initial value","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/deprecated/underscore/underscore.js","lineNumber":150,"sourceCode":"\n  // **Reduce** builds up a single result from a list of values, aka `inject`,\n  // or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.\n  _.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {\n    var initial = arguments.length > 2;\n    if (obj == null) obj = [];\n    if (nativeReduce && obj.reduce === nativeReduce) {\n      if (context) iterator = _.bind(iterator, context);\n      return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);\n    }\n    each(obj, function(value, index, list) {\n      if (!initial) {\n        memo = value;\n        initial = true;\n      } else {\n        memo = iterator.call(context, memo, value, index, list);\n      }\n    });\n    if (!initial) throw new TypeError(reduceError);\n    return memo;\n  };\n\n  // The right-associative version of reduce, also known as `foldr`.\n  // Delegates to **ECMAScript 5**'s native `reduceRight` if available.\n  _.reduceRight = _.foldr = function(obj, iterator, memo, context) {\n    var initial = arguments.length > 2;\n    if (obj == null) obj = [];\n    if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {\n      if (context) iterator = _.bind(iterator, context);\n      return initial ? obj.reduceRight(iterator, memo) : obj.reduceRight(iterator);\n    }\n    var length = obj.length;\n    if (!looksLikeArray(obj)) {\n      var keys = _.keys(obj);\n      length = keys.length;\n    }\n    each(obj, function(value, index, list) {","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/meteor/meteor/blob/5076d2f818d3cac01d0cf8312fa5b2332076a4fb/packages/deprecated/underscore/underscore.js#L132-L168","documentation":"Underscore's _.reduce / _.foldl / _.inject throw a TypeError with this message when the collection is empty and no initial value (memo) was supplied. With nothing to seed the accumulator, the fold is undefined; the error mirrors ES5 native Array.prototype.reduce behavior.","triggerScenarios":"Calling _.reduce([], fn) or _.reduce(obj, fn) where obj has no enumerable values and omitting the third memo argument.","commonSituations":"Reducing over a query/filter result that can be empty; refactoring code that previously always had data; chaining _.reduce after _.filter that yields zero items.","solutions":["Pass an explicit initial value as the third argument: _.reduce(arr, fn, initialValue).","Guard the call: if (arr.length) _.reduce(arr, fn) else useDefault().","Use native arr.reduce(fn, seed) with a seed, which behaves identically and avoids the throw."],"exampleFix":"// before\nconst total = _.reduce(items, (m, x) => m + x.value, 0);\n// but somewhere else:\n_.reduce(items, (m, x) => m + x); // no seed, empty items throws\n\n// after\nconst total = _.reduce(items, (m, x) => m + x.value, 0); // seed 0","handlingStrategy":"validation","validationCode":"function reduceSafe(arr, fn, seed) {\n  if (arguments.length < 3) {\n    if (!arr || (arr.length !== undefined ? arr.length === 0 : _.keys(arr).length === 0)) {\n      throw new TypeError('Reduce of empty array with no initial value');\n    }\n  }\n  return _.reduce.apply(null, arguments);\n}","typeGuard":"function hasInitialValue(args) {\n  return args.length > 2;\n}","tryCatchPattern":null,"preventionTips":["Always pass an explicit seed to _.reduce.","Guard reductions over potentially-empty filtered lists.","Prefer native arr.reduce(fn, seed) with a seed."],"tags":["underscore","reduce","empty-array","deprecated"],"backgroundTag":null,"analyzedSha":"5076d2f818d3cac01d0cf8312fa5b2332076a4fb","analyzedAt":"2026-08-13T03:27:40.142Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}