1

我正在尝试制作一个 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值以使其工作?

4

1 回答 1

2

好吧,你不会相信的,但我通过CreatedBy这种方式添加字段解决了它

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

是的,只需将 2 行换成ref& type!! . 交换这两行代码会破坏代码,这很奇怪:| !!!

于 2014-10-23T15:35:55.493 回答