Automattic/mongoose · error · MongooseError
Schema#pick() only accepts an array argument, got "${typeof
Error message
Schema#pick() only accepts an array argument, got "${typeof paths}" What it means
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.
Source
Thrown at lib/schema.js:524
*
* const schema = Schema({ name: String, age: Number });
* // Creates a new schema with the same `name` path as `schema`,
* // but no `age` path.
* const newSchema = schema.pick(['name']);
*
* newSchema.path('name'); // SchemaString { ... }
* newSchema.path('age'); // undefined
*
* @param {string[]} paths List of Paths to pick for the new Schema
* @param {object} [options] Options to pass to the new Schema Constructor (same as `new Schema(.., Options)`). Defaults to `this.options` if not set.
* @return {Schema}
* @api public
*/
Schema.prototype.pick = function(paths, options) {
const newSchema = new Schema({}, options || this.options);
if (!Array.isArray(paths)) {
throw new MongooseError('Schema#pick() only accepts an array argument, ' +
'got "' + typeof paths + '"');
}
for (const path of paths) {
if (this._hasEncryptedField(path)) {
const encrypt = this.encryptedFields[path];
const schemaType = this.path(path);
newSchema.add({
[path]: {
encrypt,
[this.options.typeKey]: schemaType
}
});
} else if (this.nested[path]) {
newSchema.add({ [path]: get(this.tree, path) });
} else {
const schematype = this.path(path);
if (schematype == null) {View on GitHub (pinned to 49cdab0136)
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.
Example fix
// before
const sub = schema.pick('name'); // throws: Schema#pick() only accepts an array argument, got "string"
// after
const sub = schema.pick(['name']); Defensive patterns
Strategy: type-guard
Validate before calling
function pickPaths(schema, paths) {
const list = Array.isArray(paths) ? paths : paths != null ? [paths] : [];
return schema.pick(list);
} Type guard
const isPathArray = (p) => Array.isArray(p);
Try / catch
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; } Prevention
- Always pass an array literal to pick().
- Type the argument as string[] in TypeScript.
- Default to [] when the list is optional.
When it happens
Trigger: `schema.pick('name')`; `schema.pick()` with no argument; `schema.pick({ name: 1 })`; spreading a string so it arrives as one value.
Common situations: 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.
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- Invalid value for alias option on ${prop}, got ${a}
- Invalid value for alias option on ${prop}, got ${alias}
- Path `${path}` is not in the schema
- Invalid addFields() argument. Must be an object
- Aggregate `near()` must be called with non-nullish argument
AI-assisted analysis of Automattic/mongoose@49cdab0136 (2026-08-21).
Data as JSON: /api/errors/59c45ede46e52a39.
Report an issue: GitHub.