在Mongoose插件中添加字段给出“TypeError:架构pathCreatedBy.type”的无效值

我试图做一个CreatedBy Mongoose插件,但是当试图使用ObjectId作为字段types时,它给了我( "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 ! 。 如何交换这两行可能会破坏代码是奇怪的:| !

Interesting Posts