{"record":{"id":"211ca96b3aa9a06a","repo":"Automattic/mongoose","slug":"invalid-argument","errorCode":null,"errorMessage":"invalid argument","messagePattern":"invalid argument","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"lib/query.js","lineNumber":5573,"sourceCode":"Query.prototype.near = function() {\n  const params = [];\n  const sphere = this._mongooseOptions.nearSphere;\n\n  // TODO refactor\n\n  if (arguments.length === 1) {\n    if (Array.isArray(arguments[0])) {\n      params.push({ center: arguments[0], spherical: sphere });\n    } else if (typeof arguments[0] === 'string') {\n      // just passing a path\n      params.push(arguments[0]);\n    } else if (utils.isObject(arguments[0])) {\n      if (typeof arguments[0].spherical !== 'boolean') {\n        arguments[0].spherical = sphere;\n      }\n      params.push(arguments[0]);\n    } else {\n      throw new TypeError('invalid argument');\n    }\n  } else if (arguments.length === 2) {\n    if (typeof arguments[0] === 'number' && typeof arguments[1] === 'number') {\n      params.push({ center: [arguments[0], arguments[1]], spherical: sphere });\n    } else if (typeof arguments[0] === 'string' && Array.isArray(arguments[1])) {\n      params.push(arguments[0]);\n      params.push({ center: arguments[1], spherical: sphere });\n    } else if (typeof arguments[0] === 'string' && utils.isObject(arguments[1])) {\n      params.push(arguments[0]);\n      if (typeof arguments[1].spherical !== 'boolean') {\n        arguments[1].spherical = sphere;\n      }\n      params.push(arguments[1]);\n    } else {\n      throw new TypeError('invalid argument');\n    }\n  } else if (arguments.length === 3) {\n    if (typeof arguments[0] === 'string' && typeof arguments[1] === 'number'","sourceCodeStart":5555,"sourceCodeEnd":5591,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/query.js#L5555-L5591","documentation":"Query.prototype.near() (and nearSphere(), which sets a flag and reuses this code) accepts several one-argument forms: an array of coordinates, a path string, or an options object like {center, maxDistance}. Anything else — a number, boolean, null, or a non-plain object — hits the final else and throws a bare TypeError('invalid argument').","triggerScenarios":"`query.near(5)`, `query.where('loc').near(null)`, or passing a coordinate string like `query.near('1,2')` with one argument; calling near() with a variable that is not always an array/string/object.","commonSituations":"Passing un-validated geo input from an API straight into near(); splitting a 'lat,lng' string but forgetting to map it to [lat, lng]; legacy code assuming older mongoose overloads like near(1, 1) are the only forms.","solutions":["Wrap coordinates in an array: `query.where('loc').near([1, 1])` or better `query.where('loc').near({ center: [1, 1], maxDistance: 5 })`.","Parse 'lat,lng' strings into a numeric pair array before calling near().","Prefer the object form { center, maxDistance, spherical } for clarity."],"exampleFix":"// before\nquery.where('loc').near(req.query.coords); // '40.7,-74.0' string → TypeError: invalid argument\n\n// after\nconst [lng, lat] = req.query.coords.split(',').map(Number);\nquery.where('loc').near({ center: [lng, lat], maxDistance: 5 });","handlingStrategy":"type-guard","validationCode":"function nearOneArg(query, arg) {\n  if (Array.isArray(arg) || typeof arg === 'string' || (arg != null && typeof arg === 'object')) {\n    return query.near(arg);\n  }\n  throw new TypeError('near() expects an array, string, or object');\n}","typeGuard":"const isValidNearArg = (a) => Array.isArray(a) || typeof a === 'string' || (a != null && typeof a === 'object');","tryCatchPattern":"try { query.near(input); } catch (err) { if (err instanceof TypeError && err.message === 'invalid argument') { throw new Error(`Invalid geo input for near(): ${typeof input}`); } throw err; }","preventionTips":["Parse coordinate strings into [lng, lat] number arrays before calling near().","Prefer the object form near({ center, maxDistance }).","Validate geo inputs at the API boundary."],"tags":["mongoose","geospatial","near","invalid-argument","typeerror"],"backgroundTag":"invalid-function-arguments","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}