我试图制作一个CreatedBy Mongoose插件,但是当尝试使用ObjectId作为字段类型时,它给了我("account"已经是另一个定义的集合):

TypeError: Invalid value for schema path `CreatedBy.type`


&这是插件代码:

mongoose =  require 'mongoose'
module.exports = exports = updatedByPlugin = (schema, options) ->
  schema.add CreatedBy:
    type: mongoose.Schema.Types.ObjectId
    ref: "account"
  schema.pre "save", (next) ->
    @CreatedBy = options.accountId
    next()
    return

  schema.path("CreatedBy").index options.index  if options and options.index
  return


那么,如何修改ref值使其起作用?

最佳答案

好吧,您不会相信它,但是我通过这样添加CreatedBy字段解决了它

schema.add CreatedBy:
   ref: "account"
   type: mongoose.Schema.Types.ObjectId


是的,只需将2行换成reftype! 。奇怪的是,交换这两行会破坏代码:| !!!

09-20 05:22