{"action":"closed","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/4434","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/4434/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/4434/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/4434/events","html_url":"https://github.com/Automattic/mongoose/issues/4434","id":171949120,"number":4434,"title":"Subdocuments in document with timestamps cause error when updating parent document","user":{"login":"dyang108","id":14955423,"avatar_url":"https://avatars.githubusercontent.com/u/14955423?v=3","gravatar_id":"","url":"https://api.github.com/users/dyang108","html_url":"https://github.com/dyang108","followers_url":"https://api.github.com/users/dyang108/followers","following_url":"https://api.github.com/users/dyang108/following{/other_user}","gists_url":"https://api.github.com/users/dyang108/gists{/gist_id}","starred_url":"https://api.github.com/users/dyang108/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dyang108/subscriptions","organizations_url":"https://api.github.com/users/dyang108/orgs","repos_url":"https://api.github.com/users/dyang108/repos","events_url":"https://api.github.com/users/dyang108/events{/privacy}","received_events_url":"https://api.github.com/users/dyang108/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-08-18T16:51:52Z","updated_at":"2016-08-22T02:28:50Z","closed_at":"2016-08-22T02:28:50Z","body":" When updating a saved document with timestamps, the subdocuments without options create an error, since the 'timestamps' option does not exist on the subdocument:\r\nmongoose@4.5.9, mongodb@2.1.18\r\n```\r\nvar mongoose = require('mongoose')\r\nmongoose.connect('mongodb://localhost/test')\r\nvar Schema = mongoose.Schema\r\n\r\nvar Thing = mongoose.model('Thing', new Schema({\r\n  subArray: [{\r\n    testString: String\r\n  }]\r\n}, {\r\n  timestamps: true\r\n}))\r\n\r\nvar thingy = new Thing({\r\n  subArray: [{\r\n    testString: 'not updated'\r\n  }]\r\n})\r\n\r\nvar id\r\nthingy.save(function (err, item) {\r\n  if (err) {\r\n    console.log(err)\r\n  } else {\r\n    id = item._id\r\n  }\r\n})\r\n.then(function () {\r\n  var thingy2 = {\r\n    subArray: [{\r\n      testString: 'updated'\r\n    }]\r\n  }\r\n  return Thing.update({\r\n    _id: id\r\n  }, {$set: thingy2})\r\n})\r\n.then(function () {\r\n  mongoose.connection.close()\r\n})\r\n.catch(function (err) {\r\n  console.log(err)\r\n})\r\n```\r\nthe above script gives the following error:\r\n```\r\nTypeError: Cannot read property 'createdAt' of undefined\r\n    at applyTimestampsToChildren (/Users/dyang/Documents/mongooseTest/node_modules/mongoose/lib/schema.js:859:33)\r\n    at Query.<anonymous> (/Users/dyang/Documents/mongooseTest/node_modules/mongoose/lib/schema.js:816:7)\r\n    at next (/Users/dyang/Documents/mongooseTest/node_modules/kareem/index.js:82:14)\r\n    at Kareem.execPre (/Users/dyang/Documents/mongooseTest/node_modules/kareem/index.js:99:3)\r\n    at Kareem.wrap (/Users/dyang/Documents/mongooseTest/node_modules/kareem/index.js:231:8)\r\n    at Query._execUpdate (/Users/dyang/Documents/mongooseTest/node_modules/kareem/index.js:271:11)\r\n    at Query.update (/Users/dyang/Documents/mongooseTest/node_modules/mongoose/lib/query.js:2189:17)\r\n    at /Users/dyang/Documents/mongooseTest/node_modules/mongoose/lib/query.js:2230:21\r\n    at new Promise.ES6 (/Users/dyang/Documents/mongooseTest/node_modules/mongoose/lib/promise.js:45:3)\r\n    at Query.exec (/Users/dyang/Documents/mongooseTest/node_modules/mongoose/lib/query.js:2223:10)\r\n    at Query.then (/Users/dyang/Documents/mongooseTest/node_modules/mongoose/lib/query.js:2253:15)\r\n    at resolve (/Users/dyang/Documents/mongooseTest/node_modules/mpromise/lib/promise.js:284:23)\r\n    at newTickHandler (/Users/dyang/Documents/mongooseTest/node_modules/mpromise/lib/promise.js:239:5)\r\n    at _combinedTickCallback (internal/process/next_tick.js:67:7)\r\n    at process._tickCallback (internal/process/next_tick.js:98:9)\r\n```\r\nI believe this was introduced in 4.5.9, and is related to the solution for #4049."}}
{"actor":{"display_login":"vkarpov15"}}