在Mongoose插件中添加字段会给出“TypeError:对于模式路径”CreatedBy.type“的无效值。 [英] Adding field in Mongoose plugin gives "TypeError: Invalid value for schema path `CreatedBy.type`"

查看:901
本文介绍了在Mongoose插件中添加字段会给出“TypeError:对于模式路径”CreatedBy.type“的无效值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要创建一个CreatedBy Mongoose插件,但是当尝试使用 ObjectId 作为字段类型时,它会给我( 是另一个已定义的集合):

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

那么如何修改 ref value to make it work?

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

推荐答案

好吧,你不会相信它, CreatedBy 此方式

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

只是换两行 ref & type ! 。这是奇怪的如何交换这两行可以打破代码: !!!

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

这篇关于在Mongoose插件中添加字段会给出“TypeError:对于模式路径”CreatedBy.type“的无效值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆