Mongoose版本更新后出现验证错误:owner.$populated不是函数

9rbhqvlz  于 2022-11-13  发布在  Go
关注(0)|答案(1)|浏览(93)

更新到Mongoose(6.6.1)的最新版本后,文档的.保存()现在出现错误,而之前的保存没有问题。这是关于一个字段的验证,标记为Date,但对我来说似乎没问题。无论如何,我得到了错误owner.$populated is not a function
问题是什么?为什么错误涉及owner字段?

这是对象

new MyTask({
        "source": {
                    "client_id" : "ABC",
                    "ip"        : "127.0.0.1"
                  },
       "invoice": {
                    "amount"    : 1000,
                    "currency"  : "eur",
                    "dueAt"     : "2022-10-23T14:55:57.482Z",
                    "issuedAt"  : "2022-07-25T13:55:57.480Z",
                    "number"    : "724/001"        
                  }
    }).save();

这是结构描述

var invoice_schema = new Schema({
                "amount"   : {type:Number,    required:true},
                "currency" : {type:String,    required:true}, 
                "dueAt"    : {type:Date,      required:true}, 
                "issuedAt" : {type:Date,      required:true},
                "number"   : {type:String}                    
                                },
               {
                timestamps:true,
                toObject  : { virtuals: true },
                toJSON    :   { virtuals: true }  
               });

var MYTASK_SCHEMA = new Schema(
  {
   source : {
              ip         : {type: String},
              client_id  : {type: String}
             },        
   invoice: {type        : invoice_schema}
  },
  {
   timestamps:true,
   toObject: { virtuals: true },
   toJSON  : { virtuals: true }  
  });

这是错误:

Error: MyTask validation failed: 
          invoice.dueAt: owner.$populated is not a function, 
          invoice.issuedAt: owner.$populated is not a function, 
          invoice: Validation failed: 
              dueAt: owner.$populated is not a function, 
              issuedAt: owner.$populated is not a function
    at ValidationError.inspect (/app/node_modules/mongoose/lib/error/validation.js:48:26)
    at formatValue (node:internal/util/inspect:790:19)
    at inspect (node:internal/util/inspect:350:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2241:40)
    at formatWithOptions (node:internal/util/inspect:2103:10)
    at console.value (node:internal/console/constructor:348:14)
    at console.warn (node:internal/console/constructor:381:61)
13z8s7eq

13z8s7eq1#

在一些检查之后,我发现package.json中列出的一个包与mongoose的一个旧版本有依赖关系。

$npm list mongoose

servers@1.0.0 /app
    +-- @mypackage/api_core@1.0.36
    | +-- @mypackage/misc@1.0.29
    | | `-- mongoose@6.6.1 deduped
    | +-- @mypackage/task@1.0.21
    | | `-- mongoose@5.8.11      /* <-- this one broke everything */
    | `-- @mypackage/user@1.0.12
    |   `-- mongoose@6.6.1 deduped
    +-- @mypackage/auth@1.0.6
    | +-- @mypackage/bank@1.0.14
    | | `-- mongoose@6.6.1 deduped

更新该软件包以使用最新版本的Mongoose,似乎工作!

相关问题