【问题标题】:Typegoose enum arrayProp returns an error: Type is not a constructorTypegoose enum arrayProp 返回错误:Type is not a constructor
【发布时间】:2020-12-05 17:06:22
【问题描述】:

我的数组架构定义有问题。所以基本上,我想要实现的是一个具有名为游戏的属性的单用户模型,它包含用户正在玩的一系列游戏。问题是我将游戏定义为枚举:

module Constants {
    export enum Games {
        LOL = 'League Of Legends',
    }
}

导出{常量};

现在,当我尝试将它附加到这样的架构模型时:

@arrayProp({ required: true, items: Constants.Games })
games: Constants.Games[];

我收到一个错误(在成功编译之后,就在服务器启动之前)

^ const instance = new Type();
TypeError: Type is not a constructor
    at baseProp (C:\Users\Borys\Desktop\lft\backend\node_modules\typegoose\lib\prop.js:114:22)
    at C:\Users\Borys\Desktop\lft\backend\node_modules\typegoose\lib\prop.js:177:9
    at DecorateProperty (C:\Users\Borys\Desktop\lft\backend\node_modules\reflect-metadata\Reflect.js:553:33)
    at Object.decorate (C:\Users\Borys\Desktop\lft\backend\node_modules\reflect-metadata\Reflect.js:123:24)
    at __decorate (C:\Users\Borys\Desktop\lft\backend\build\dataModel\User.js:4:92)
    at Object.<anonymous> (C:\Users\Borys\Desktop\lft\backend\build\dataModel\User.js:64:1)

我已经阅读了一些有关此错误的信息,但它与 items/itemsRef 的 required 选项有关.

任何人都可以帮助/联系?

【问题讨论】:

    标签: node.js typescript typegoose


    【解决方案1】:
    @arrayProp({ required: true, items: String })
    games: Constants.Games[];
    

    是解决这个问题的方法。

    如果有人能澄清并告诉我为什么不应该在 items 属性中使用枚举,我将不胜感激。

    【讨论】:

      【解决方案2】:

      问题是,你不能使用枚举作为运行时的猫鼬类型,所以我建议使用

      @prop({ required: true, type: String, enum: Constants.Games })
      games: Constants.Games[];
      
      • type 用于设置类型(即string)(此选项可以省略 - 感谢反射)
      • enum 用于设置要验证的枚举

      【讨论】:

      • 谢谢你,我相信没有人会比你更清楚正确答案。 :) 我会记下这一点。
      猜你喜欢
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      • 2022-12-05
      • 2021-12-23
      • 2019-07-15
      • 2018-05-24
      • 1970-01-01
      • 2022-12-02
      相关资源
      最近更新 更多