Adding field in Mongoose plugin gives “TypeError: Invalid value for schema path `CreatedBy.type`”

*爱你&永不变心* 提交于 2019-12-11 11:14:38

问题


I'm trying to make a CreatedBy Mongoose plugin, but when trying to use the ObjectId as the field type it gives me ("account" is another defined collection already):

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

& here is the plugin code:

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

So how I can modify the ref value to make it work?


回答1:


Well, you won't believe it, but I solved it by adding the CreatedBy field this way

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

Yes, by just exchanging the 2 lines for ref & type!! . It is weird how exchanging these 2 lines could break the code :| !!!



来源:https://stackoverflow.com/questions/26511604/adding-field-in-mongoose-plugin-gives-typeerror-invalid-value-for-schema-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!