{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3819","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3819/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3819/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3819/events","html_url":"https://github.com/Automattic/mongoose/issues/3819","id":129646682,"number":3819,"title":"Event `pre.set` doesn't modified document value","user":{"login":"marchah","id":818848,"avatar_url":"https://avatars.githubusercontent.com/u/818848?v=3","gravatar_id":"","url":"https://api.github.com/users/marchah","html_url":"https://github.com/marchah","followers_url":"https://api.github.com/users/marchah/followers","following_url":"https://api.github.com/users/marchah/following{/other_user}","gists_url":"https://api.github.com/users/marchah/gists{/gist_id}","starred_url":"https://api.github.com/users/marchah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marchah/subscriptions","organizations_url":"https://api.github.com/users/marchah/orgs","repos_url":"https://api.github.com/users/marchah/repos","events_url":"https://api.github.com/users/marchah/events{/privacy}","received_events_url":"https://api.github.com/users/marchah/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2016-01-29T02:37:08Z","updated_at":"2016-02-03T02:09:19Z","closed_at":null,"body":"Hi,\r\n\r\nI have an issue when i declare a middleware for the event `pre.set`:\r\n on the document `EventEmitter`, the event `set` is not register, so when you modify the value in `pre.save`, the changes aren't applied to the document.\r\n\r\nFile to test:\r\n````javascript\r\nvar mongoose = require('mongoose');\r\n\r\nvar NestedSchema = new mongoose.Schema({\r\n  'label_1': {\r\n    'type': String,\r\n    'required': true,\r\n  },\r\n  'label_2': {\r\n    'type': String,\r\n    'required': false,\r\n  },\r\n});\r\n\r\nvar Nested = mongoose.model('Nested', NestedSchema);\r\n\r\nvar TestSchema = new mongoose.Schema({\r\n  'field': {\r\n    'type': String,\r\n    'required': true,\r\n  },\r\n  'nested': {\r\n    'type': mongoose.Schema.ObjectId,\r\n    'ref': 'Nested1',\r\n    'required': true,\r\n  },\r\n});\r\n\r\n\r\nTestSchema.pre('set', function preSet(next, path, val, typel) {\r\n  if (path === 'nested') {\r\n    val = new Nested({\r\n      'label_1': 'embedded_1',\r\n      'label_2': 'embedded_2',\r\n    });\r\n  }\r\n  console.log('EMIT', val);\r\n  this.emit('set', path, val);\r\n  console.log('NEXT');\r\n  next();\r\n});\r\n\r\nvar Test = mongoose.model('Test', TestSchema);\r\n\r\nvar tmp = new Test();\r\n\r\ntmp.nested = {\r\n  'label_1': 'label_1',\r\n  'label_2': 'label_2',\r\n};\r\n````\r\n\r\n - add `console.log('EVENTS', this.$__.emitter._events);` [here](https://github.com/Automattic/mongoose/blob/master/lib/document.js#L90), it won't have the event `set`.\r\n\r\n- add `console.log('SET', val);` [here](https://github.com/Automattic/mongoose/blob/master/lib/document.js#L445), you will have the old value instead of the one set in `pre.set`.\r\n\r\nOutput:\r\n````\r\nEMIT { _id: 56aad0dfe87c7b030026a13b,\r\n  label_2: 'embedded_2',\r\n  label_1: 'embedded_1' }\r\nEVENTS {}\r\nNEXT\r\nSET { label_1: 'label_1', label_2: 'label_2' }\r\n````\r\nThanks"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/178957997","html_url":"https://github.com/Automattic/mongoose/issues/3819#issuecomment-178957997","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3819","id":178957997,"user":{"login":"marchah","id":818848,"avatar_url":"https://avatars.githubusercontent.com/u/818848?v=3","gravatar_id":"","url":"https://api.github.com/users/marchah","html_url":"https://github.com/marchah","followers_url":"https://api.github.com/users/marchah/followers","following_url":"https://api.github.com/users/marchah/following{/other_user}","gists_url":"https://api.github.com/users/marchah/gists{/gist_id}","starred_url":"https://api.github.com/users/marchah/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marchah/subscriptions","organizations_url":"https://api.github.com/users/marchah/orgs","repos_url":"https://api.github.com/users/marchah/repos","events_url":"https://api.github.com/users/marchah/events{/privacy}","received_events_url":"https://api.github.com/users/marchah/received_events","type":"User","site_admin":false},"created_at":"2016-02-03T02:09:19Z","updated_at":"2016-02-03T02:09:19Z","body":"@vkarpov15 So there is any way to \"convert it and trigger another set\" in a hook/event?\r\nI also thought about using `this._doc` but i will loose all the schema internal verifications."}}