{"record":{"id":"59c45ede46e52a39","repo":"Automattic/mongoose","slug":"schema-pick-only-accepts-an-array-argument-got","errorCode":null,"errorMessage":"Schema#pick() only accepts an array argument, got \"${typeof paths}\"","messagePattern":"Schema#pick\\(\\) only accepts an array argument, got \"(.+?)\"","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/schema.js","lineNumber":524,"sourceCode":" *\n *     const schema = Schema({ name: String, age: Number });\n *     // Creates a new schema with the same `name` path as `schema`,\n *     // but no `age` path.\n *     const newSchema = schema.pick(['name']);\n *\n *     newSchema.path('name'); // SchemaString { ... }\n *     newSchema.path('age'); // undefined\n *\n * @param {string[]} paths List of Paths to pick for the new Schema\n * @param {object} [options] Options to pass to the new Schema Constructor (same as `new Schema(.., Options)`). Defaults to `this.options` if not set.\n * @return {Schema}\n * @api public\n */\n\nSchema.prototype.pick = function(paths, options) {\n  const newSchema = new Schema({}, options || this.options);\n  if (!Array.isArray(paths)) {\n    throw new MongooseError('Schema#pick() only accepts an array argument, ' +\n      'got \"' + typeof paths + '\"');\n  }\n\n  for (const path of paths) {\n    if (this._hasEncryptedField(path)) {\n      const encrypt = this.encryptedFields[path];\n      const schemaType = this.path(path);\n      newSchema.add({\n        [path]: {\n          encrypt,\n          [this.options.typeKey]: schemaType\n        }\n      });\n    } else if (this.nested[path]) {\n      newSchema.add({ [path]: get(this.tree, path) });\n    } else {\n      const schematype = this.path(path);\n      if (schematype == null) {","sourceCodeStart":506,"sourceCodeEnd":542,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schema.js#L506-L542","documentation":"Schema.prototype.pick(paths) builds a new schema containing only the listed paths, and it requires `paths` to be an array. Passing a single string, undefined, or any non-array throws immediately with the actual typeof in the message. There is no single-string convenience overload.","triggerScenarios":"`schema.pick('name')`; `schema.pick()` with no argument; `schema.pick({ name: 1 })`; spreading a string so it arrives as one value.","commonSituations":"Assuming pick mirrors lodash-style single-value APIs; optional-path code where the array is sometimes undefined; refactoring field lists into variables that lose their array wrapper.","solutions":["Wrap the path(s) in an array: `schema.pick(['name'])`.","Default missing input to an empty array: `schema.pick(paths ?? [])` if an empty pick schema is intended.","Type the parameter as string[] in TS so the compiler catches misuse."],"exampleFix":"// before\nconst sub = schema.pick('name'); // throws: Schema#pick() only accepts an array argument, got \"string\"\n\n// after\nconst sub = schema.pick(['name']);","handlingStrategy":"type-guard","validationCode":"function pickPaths(schema, paths) {\n  const list = Array.isArray(paths) ? paths : paths != null ? [paths] : [];\n  return schema.pick(list);\n}","typeGuard":"const isPathArray = (p) => Array.isArray(p);","tryCatchPattern":"try { return schema.pick(paths); } catch (err) { if (err instanceof mongoose.Error && /only accepts an array/.test(err.message)) { return schema.pick([paths]); } throw err; }","preventionTips":["Always pass an array literal to pick().","Type the argument as string[] in TypeScript.","Default to [] when the list is optional."],"tags":["mongoose","schema","pick","invalid-argument"],"backgroundTag":"invalid-argument-type","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}