【问题标题】:Mongoose JobSchema.pre('update', function(n){n()}) throws: TypeError: Cannot read property 'numAsyncPres' of undefinedMongoose JobSchema.pre('update', function(n){n()}) throws: TypeError: Cannot read property 'numAsyncPres' of undefined
【发布时间】:2012-07-30 12:01:25
【问题描述】:

我在 mongoose 中有这个 Schema,当我使用带有更新的 pre 时,我得到了这个错误。

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

注意事项:

  • pre('保存'工作
  • post('update' 不会抛出错误并且不起作用

【问题讨论】:

    标签: mongoose pre


    【解决方案1】:

    我发现了这个:https://github.com/LearnBoost/mongoose/issues/538 所以没有更新前...

    【讨论】:

      【解决方案2】:

      根据Mongoose documentation,pre和post中间件功能支持:

      • 初始化
      • 验证
      • 保存
      • 移除

      不支持更新。

      【讨论】:

      • Greg,假设我有一个更新用户功能,你能指导我如何在 更新用户密码时再次散列 用户密码,我曾经在创建用户时这样做,在 pre save hook
      【解决方案3】:

      确实,除了上面列出的 Greg 之外,Mongoose 不支持模型 API 上的钩子。但是,可以通过 Monkey-patch 完成更新挂钩。 Hooker NPM 包是一种干净利落的好方法。

      RESTeasy 项目是 Node 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】:

        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');
        });
        

        注意事项:

        "Query 中间件与 document 中间件的区别在于微妙但 重要方式:在文档中间件中,this指的是文档 正在更新。在查询中间件中,猫鼬不一定有 对正在更新的文档的引用,因此 this 指的是 查询 对象 而不是正在更新的文档。”

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-25
        • 2017-04-24
        • 2023-03-15
        • 2021-09-18
        • 2020-11-06
        • 2021-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多