作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 mongoose 中有这个架构,当我使用 pre 和 update 时,出现此错误。
JobSchema.pre('update', function(n){n()})
C:\web\production01_server\node_modules\production\node_modules\mongoose\lib\utils.js:413
throw err;
^
TypeError: Cannot read property 'numAsyncPres' of undefined
at Model._lazySetupHooks (C:\web\production01_server\node_modules\production\node_modules\mongoose\node_modules\hooks\hooks.js:149:49)
at Model.pre (C:\web\production01_server\node_modules\production\node_modules\mongoose\node_modules\hooks\hooks.js:113:10)
at Model.doQueue (C:\web\production01_server\node_modules\production\node_modules\mongoose\lib\document.js:1116:41)
at Model.Document (C:\web\production01_server\node_modules\production\node_modules\mongoose\lib\document.js:55:8)
at Model.Model (C:\web\production01_server\node_modules\production\node_modules\mongoose\lib\model.js:26:12)
at Model.model (C:\web\production01_server\node_modules\production\node_modules\mongoose\lib\model.js:910:11)
at new Model (C:\web\production01_server\node_modules\production\node_modules\mongoose\lib\connection.js:418:15)
at cb (C:\web\production01_server\node_modules\production\node_modules\mongoose\lib\query.js:804:16)
at C:\web\production01_server\node_modules\production\node_modules\mongoose\lib\utils.js:408:16
at C:\web\production01_server\node_modules\production\node_modules\mongoose\node_modules\mongodb\lib\mongodb\cursor.js:133:9
最佳答案
Mongoose 4.0 通过查询中间件支持预更新 Hook 。
http://mongoosejs.com/docs/middleware.html
schema.pre('update', function() {
console.log(this instanceof mongoose.Query); // true
this.start = Date.now();
});
schema.post('update', function() {
console.log(this instanceof mongoose.Query); // true
console.log('update() took ' + (Date.now() - this.start) + ' millis');
});
"Query middleware differs from document middleware in a subtle but important way: in document middleware, this refers to the document being updated. In query middleware, mongoose doesn't necessarily have a reference to the document being updated, so this refers to the query object rather than the document being updated."
关于Mongoose JobSchema.pre ('update' , function(n){n()}) 抛出 : TypeError: Cannot read property 'numAsyncPres' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11761819/
我是一名优秀的程序员,十分优秀!