{"action":"closed","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/4648","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/4648/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/4648/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/4648/events","html_url":"https://github.com/Automattic/mongoose/issues/4648","id":184714420,"number":4648,"title":"Is it possible to access static methods of sub/nested documents?","user":{"login":"kdmon","id":707799,"avatar_url":"https://avatars.githubusercontent.com/u/707799?v=3","gravatar_id":"","url":"https://api.github.com/users/kdmon","html_url":"https://github.com/kdmon","followers_url":"https://api.github.com/users/kdmon/followers","following_url":"https://api.github.com/users/kdmon/following{/other_user}","gists_url":"https://api.github.com/users/kdmon/gists{/gist_id}","starred_url":"https://api.github.com/users/kdmon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kdmon/subscriptions","organizations_url":"https://api.github.com/users/kdmon/orgs","repos_url":"https://api.github.com/users/kdmon/repos","events_url":"https://api.github.com/users/kdmon/events{/privacy}","received_events_url":"https://api.github.com/users/kdmon/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-10-23T20:42:33Z","updated_at":"2016-10-26T16:22:01Z","closed_at":"2016-10-26T16:22:01Z","body":"I'm trying to access a static method belonging to a sub-document (``getAuthenticated()``, part of the User schema). \r\n\r\nCompany is the root schema and pulls in the User schema. I have an authentication middleware where I want to do something like: Company.users.getAuthenticated(...), but users (and getAuthenticated) are undefined. Any pointers are highly appreciated!\r\n\r\nmodels/user.js:\r\n```\r\n[---]\r\nvar UserSchema = new mongoose.Schema({\r\n  created:       { type: Date, default: Date.now },\r\n  username:      { type: String, required: true, unique: true},\r\n  email:         { type: String, required: true, unique: true, lowercase: true, trim: true},\r\n  password:      { type: String, required: true}\r\n});\r\n\r\n[---] \r\n\r\nUserSchema.statics.getAuthenticated = function(username, password, cb) {\r\n  this.findOne({ username: username }, function(err, user) {\r\n    if (err) return cb(err);\r\n\r\n    // make sure the user exists\r\n    if (!user) {console.log (\"not found\");}\r\n   }\r\n}\r\n[---] \r\n\r\nmodule.exports = UserSchema;\r\n\r\n```\r\n\r\nmodels/company.js:\r\n\r\n```\r\n[---]\r\nvar UserSchema     = require('./user');\r\nvar CompanySchema = new mongoose.Schema({\r\n  name: {type: String, required: true},\r\n  users: [UserSchema]\r\n});\r\n\r\nmodule.exports = mongoose.model('Company', CompanySchema);\r\n\r\n```\r\n\r\nmiddleware/authentication.js:\r\n\r\n```\r\n[---]\r\nvar Company       = require('../models/company');\r\n\r\npassport.use('userAuthentication', new LocalStrategy(\r\n  function(username, password, cb) {\r\n    Company.users.getAuthenticated(username, password, function (err, user) {\r\n      console.log (err);\r\n      if (err) { return cb(err); }\r\n      if (!user) { return cb(null, false); }\r\n      return cb(null, user);\r\n    });\r\n  })\r\n);\r\n[---]\r\n```\r\n\r\n\r\n"}}
{"actor":{"display_login":"vkarpov15"}}