{"action":"closed","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/5897","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/5897/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/5897/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/5897/events","html_url":"https://github.com/Automattic/mongoose/issues/5897","id":281290669,"number":5897,"title":"Pre hooks and timestamps not triggered","user":{"login":"bitflower","id":6666636,"avatar_url":"https://avatars2.githubusercontent.com/u/6666636?v=4","gravatar_id":"","url":"https://api.github.com/users/bitflower","html_url":"https://github.com/bitflower","followers_url":"https://api.github.com/users/bitflower/followers","following_url":"https://api.github.com/users/bitflower/following{/other_user}","gists_url":"https://api.github.com/users/bitflower/gists{/gist_id}","starred_url":"https://api.github.com/users/bitflower/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bitflower/subscriptions","organizations_url":"https://api.github.com/users/bitflower/orgs","repos_url":"https://api.github.com/users/bitflower/repos","events_url":"https://api.github.com/users/bitflower/events{/privacy}","received_events_url":"https://api.github.com/users/bitflower/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/Automattic/mongoose/milestones/197","html_url":"https://github.com/Automattic/mongoose/milestone/197","labels_url":"https://api.github.com/repos/Automattic/mongoose/milestones/197/labels","id":2973317,"number":197,"title":"4.13.8","description":"","creator":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars2.githubusercontent.com/u/1620265?v=4","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"open_issues":10,"closed_issues":3,"state":"open","created_at":"2017-12-12T01:48:43Z","updated_at":"2017-12-20T22:28:28Z","due_on":null,"closed_at":null},"comments":3,"created_at":"2017-12-12T08:00:52Z","updated_at":"2017-12-20T22:28:28Z","closed_at":"2017-12-20T22:28:28Z","author_association":"NONE","body":"**Do you want to request a *feature* or report a *bug*?**\r\nBug.\r\n\r\n**What is the current behavior?**\r\nNo pre hooks are triggered. Also the timestamps are not updated.\r\n\r\n**If the current behavior is a bug, please provide the steps to reproduce.**\r\n## Schema (typescript):\r\n```typescript\r\nimport { Schema, SchemaOptions, SchemaDefinition } from 'mongoose';\r\n\r\nconst schemaOptions: SchemaOptions = {\r\n  timestamps: {\r\n    createdAt: 'created',\r\n    updatedAt: 'updated'\r\n  },\r\n  retainKeyOrder: true // http://mongoosejs.com/docs/guide.html#retainKeyOrder\r\n};\r\n\r\nconst schemaDefinition: SchemaDefinition = {\r\n  version: {\r\n    type: Number,\r\n    default: 1.0 // Application version\r\n  },\r\n  created: {\r\n    type: Date,\r\n    default: Date.now\r\n  },\r\n  updated: {\r\n    type: Date,\r\n    default: Date.now\r\n  }\r\n};\r\n\r\nconst BASE_SCHEMA: Schema = new Schema(schemaDefinition, schemaOptions);\r\n\r\n// /******************\r\n//  * Pre-save hook\r\n//  ******************/\r\n\r\nconsole.log('WOOOOP');\r\n\r\nBASE_SCHEMA.pre('validate', beforeSave);\r\nBASE_SCHEMA.pre('save', beforeSave);\r\nBASE_SCHEMA.pre('update', beforeSave);\r\nBASE_SCHEMA.pre('findOneAndUpdate', beforeSave);\r\nfunction beforeSave(next: Function): void {\r\n  console.log('MÖÖÖP');\r\n  // Set dates\r\n  const now: Date = new Date();\r\n  this.updated = now;\r\n  if (!this.created) {\r\n    this.created = now;\r\n}\r\nnext();\r\n}\r\n\r\nexport { BASE_SCHEMA };\r\n```\r\nIt is defined in an external node module which is being imported into an express app. Model instantiation, data creating, loading etc all works fine. Only the pre hooks and timestamps features don't work.\r\n\r\nAnd this is a derived schema (via plugin mongoose-extend-schema recommended by mongoose team) that is used to instantiate models:\r\n```typescript\r\nimport { Document } from 'mongoose';\r\nimport * as extendSchema from 'mongoose-extend-schema';\r\nimport { BASE_SCHEMA } from './base.schema';\r\nimport {\r\n  REPOSITORY_FIELD_SCHEMA,\r\n  IRepositoryField\r\n} from './repository-field.schema';\r\n\r\nexport interface IRepositorySchema extends Document {\r\n  name: String;\r\n  namespace: String;\r\n  type: String;\r\n  fields: any[];\r\n}\r\n\r\nexport class RepositoryItem {\r\n  _id: string;\r\n  name: string;\r\n  namespace: string;\r\n  fields: IRepositoryField[];\r\n}\r\n\r\nconst repositoryFields: any = {\r\n  name: {\r\n    type: String,\r\n    required: true,\r\n    trim: true\r\n  },\r\n  namespace: {\r\n    type: String,\r\n    required: true,\r\n    trim: true,\r\n    lowercase: true,\r\n    index: {\r\n      unique: true,\r\n      background: false\r\n    }\r\n    // validate: nameSpaceValidator\r\n  },\r\n  type: {\r\n    type: String,\r\n    default: 'coCustomCollection' // später weitere Optionen: 'REST-Resource', etc...\r\n  },\r\n  fields: [REPOSITORY_FIELD_SCHEMA]\r\n};\r\nexport const REPOSITORY_SCHEMA = extendSchema(BASE_SCHEMA, repositoryFields);\r\n\r\n```\r\n\r\nI have already tried to put the ```pre```hooks on the derived ```REPOSITORY_SCHEMA``` schema - no effect!\r\n\r\n## Model instantiation\r\n```typescript\r\nexport function instantiateDataModel(\r\n  mongooseInstance: Mongoose,\r\n  repository: string\r\n): Promise<Model<IRepositorySchema>> {\r\n  return new Promise(resolve => {\r\n    // Check if model was already created\r\n    if (mongooseInstance.modelNames().indexOf(repository) < 0) {\r\n      // Create dynamic model\r\n      getDataSchema(mongooseInstance, repository).then((schema: Schema) => {\r\n        resolve(\r\n          mongooseInstance.model<IRepositorySchema>(\r\n            repository,\r\n            schema,\r\n            repository\r\n          )\r\n        );\r\n      });\r\n    } else {\r\n      // Use existing model\r\n      resolve(mongooseInstance.model<IRepositorySchema>(repository));\r\n    }\r\n  });\r\n}\r\n```\r\n\r\n```getDataSchema``` return the schema (a mix of the above BASE_SCHEMA and a dynamically filled ´´´fields```field), ```repository```is just a string. Like I said: I can write data, read it, delete it. All good. Except the hooks.....\r\n\r\n**What is the expected behavior?**\r\nPre hook to be called and the timestamps to be set.\r\n\r\n**Please mention your node.js, mongoose and MongoDB version.**\r\nMongoose Version: 4.13.6\r\nNode: 6.12.0\r\nMongo DB: 3.4.10"}}
{"actor":{"display_login":"vkarpov15"}}