I have this Schema in mongoose and when I use the pre with update, I get this error.
我在mongoose中有这个模式,当我使用pre和update时,我就会得到这个错误。
JobSchema.pre('update', function(n){n()})
Full error
完整的错误
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
Notes:
注:
- pre('save' WORKS
- (“保存”
- post('update' Doesn't throw error and doesn't works
- post('update'不会抛出错误,也不会起作用)。
4 个解决方案
#1
9
Mongoose 4.0 supports pre update hooks via Query middleware. http://mongoosejs.com/docs/middleware.html
Mongoose 4.0通过查询中间件支持预更新钩子。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');
});
A note of caution:
值得注意的是:
"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并不一定要对正在更新的文档有引用,所以这指的是查询对象,而不是正在更新的文档。
#2
4
According to the Mongoose documentation, the pre and post middleware functions support:
根据Mongoose文档,pre和post中间件功能支持:
- init
- 初始化
- validate
- 验证
- save
- 保存
- remove
- 删除
No support for update.
不支持更新。
#3
3
It's true that Mongoose doesn't support hooks on Model APIs other than those Greg listed above. However, an update hook can be done via Monkey-patch. The Hooker NPM package is a good way of doing this cleanly.
确实,Mongoose不支持其他模型api上的钩子。但是,更新钩子可以通过Monkey-patch完成。Hooker NPM软件包是一个很好的方法。
The RESTeasy project which is a Boilerplate for Node REST APIs has code that demonstrates how to do it:
RESTeasy项目是节点REST api的样板,它的代码演示了如何做:
var hooker = require('hooker');
var BaseSchema = new mongoose.Schema({
sampleString: {
type: String
}
});
var BaseModel = mongoose.model('Base', BaseSchema);
// Utilize hooks for update operations. We do it in this way because MongooseJS
// does not natively support update hooks at the Schema level. This is a way
// to support it.
hooker.hook (BaseModel, 'update', {
pre: function () {
// Insert any logic you want before updating to occur here
console.log('BaseModel pre update');
},
post: function () {
// Insert any logic you want after updating to occur here
console.log('BaseModel post update');
}
});
// Export the Mongoose model
module.exports = BaseModel;
#4
1
I found this: https://github.com/LearnBoost/mongoose/issues/538 So no pre for update...
我发现了这个:https://github.com/learnboost/mongoose/es/538,所以没有更新的pre…
#1
9
Mongoose 4.0 supports pre update hooks via Query middleware. http://mongoosejs.com/docs/middleware.html
Mongoose 4.0通过查询中间件支持预更新钩子。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');
});
A note of caution:
值得注意的是:
"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并不一定要对正在更新的文档有引用,所以这指的是查询对象,而不是正在更新的文档。
#2
4
According to the Mongoose documentation, the pre and post middleware functions support:
根据Mongoose文档,pre和post中间件功能支持:
- init
- 初始化
- validate
- 验证
- save
- 保存
- remove
- 删除
No support for update.
不支持更新。
#3
3
It's true that Mongoose doesn't support hooks on Model APIs other than those Greg listed above. However, an update hook can be done via Monkey-patch. The Hooker NPM package is a good way of doing this cleanly.
确实,Mongoose不支持其他模型api上的钩子。但是,更新钩子可以通过Monkey-patch完成。Hooker NPM软件包是一个很好的方法。
The RESTeasy project which is a Boilerplate for Node REST APIs has code that demonstrates how to do it:
RESTeasy项目是节点REST api的样板,它的代码演示了如何做:
var hooker = require('hooker');
var BaseSchema = new mongoose.Schema({
sampleString: {
type: String
}
});
var BaseModel = mongoose.model('Base', BaseSchema);
// Utilize hooks for update operations. We do it in this way because MongooseJS
// does not natively support update hooks at the Schema level. This is a way
// to support it.
hooker.hook (BaseModel, 'update', {
pre: function () {
// Insert any logic you want before updating to occur here
console.log('BaseModel pre update');
},
post: function () {
// Insert any logic you want after updating to occur here
console.log('BaseModel post update');
}
});
// Export the Mongoose model
module.exports = BaseModel;
#4
1
I found this: https://github.com/LearnBoost/mongoose/issues/538 So no pre for update...
我发现了这个:https://github.com/learnboost/mongoose/es/538,所以没有更新的pre…