{"record":{"id":"81895d33173ae134","repo":"discordjs/discord.js","slug":"fn-is-not-a-function","errorCode":null,"errorMessage":"${fn} is not a function","messagePattern":"(.+?) is not a function","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/collection/src/collection.ts","lineNumber":294,"sourceCode":"\t * @example\n\t * ```ts\n\t * collection.find(user => user.username === 'Bob');\n\t * ```\n\t */\n\tpublic find<NewValue extends Value>(\n\t\tfn: (value: Value, key: Key, collection: this) => value is NewValue,\n\t): NewValue | undefined;\n\tpublic find(fn: (value: Value, key: Key, collection: this) => unknown): Value | undefined;\n\tpublic find<This, NewValue extends Value>(\n\t\tfn: (this: This, value: Value, key: Key, collection: this) => value is NewValue,\n\t\tthisArg: This,\n\t): NewValue | undefined;\n\tpublic find<This>(\n\t\tfn: (this: This, value: Value, key: Key, collection: this) => unknown,\n\t\tthisArg: This,\n\t): Value | undefined;\n\tpublic find(fn: (value: Value, key: Key, collection: this) => unknown, thisArg?: unknown): Value | undefined {\n\t\tif (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`);\n\t\tif (thisArg !== undefined) fn = fn.bind(thisArg);\n\t\tfor (const { 0: key, 1: value } of this) {\n\t\t\tif (fn(value, key, this)) return value;\n\t\t}\n\n\t\treturn undefined;\n\t}\n\n\t/**\n\t * Searches for the key of a single item where the given function returns a truthy value. This behaves like\n\t * {@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex | Array.findIndex()},\n\t * but returns the key rather than the positional index.\n\t *\n\t * @param fn - The function to test with (should return a boolean)\n\t * @param thisArg - Value to use as `this` when executing the function\n\t * @example\n\t * ```ts\n\t * collection.findKey(user => user.username === 'Bob');","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/discordjs/discord.js/blob/a81ed8a306d37fdc746e26a634b6a42164ba2c8c/packages/collection/src/collection.ts#L276-L312","documentation":"Collection.find(predicate, thisArg?) iterates the collection and returns the first value for which the predicate is truthy. It explicitly type-checks the predicate and throws a TypeError (with the stringified argument in the message) when fn is not a function, giving a clear failure instead of an opaque 'fn is not a function' crash deeper in the loop.","triggerScenarios":"Calling collection.find with a non-callable first argument — e.g. passing an object, a string property name, or an undefined variable that was expected to be a predicate; or accidentally passing the predicate as the second argument (thisArg slot) leaving fn non-function.","commonSituations":"Mixing up argument order (collection.find(thisArg, fn)); passing a value where a predicate is required after refactoring from Array.prototype.find wrappers; undefined callback due to a typo'd import; lodash-style shorthand predicates (strings/objects) which this Collection does not support.","solutions":["Pass an actual predicate function: collection.find((value, key) => ...).","Check argument order — the predicate comes first, optional thisArg second.","Verify the callback variable is defined/imported and not undefined at call time.","If using lodash-style shorthand, rewrite it as an explicit arrow function predicate."],"exampleFix":"// before\ncollection.find({ name: 'foo' }); // lodash-style, unsupported\n// after\ncollection.find((value) => value.name === 'foo');","handlingStrategy":"type-guard","validationCode":"if (typeof predicate !== 'function') {\n  throw new TypeError('find() expects (value, key, collection) => unknown');\n}","typeGuard":"function isPredicate<K, V>(f: unknown): f is (value: V, key: K, collection: unknown) => unknown {\n  return typeof f === 'function';\n}","tryCatchPattern":"try {\n  const found = collection.find(fn);\n} catch (err) {\n  if (err instanceof TypeError && err.message.includes('is not a function')) {\n    console.error('find() first argument must be a predicate function.');\n  } else throw err;\n}","preventionTips":["Never rely on lodash-style shorthand predicates; write explicit arrow functions.","Check argument order: predicate first, thisArg second.","Type the callback with TypeScript so non-functions fail at compile time."],"tags":["collection","typeerror","callback"],"backgroundTag":"callback-not-a-function","analyzedSha":"a81ed8a306d37fdc746e26a634b6a42164ba2c8c","analyzedAt":"2026-08-30T04:07:22.193Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}