【发布时间】:2020-11-25 09:56:56
【问题描述】:
我在使用 typegoose 时遇到问题, 我有这样的课:
class UserType1 extends Typegoose
implements User{
@prop({required: true, unique: true})
_username: string | undefined;
@prop({required: true})
_password: string | undefined;
constructor(username:string, password: string){
this._username = username;
this._password = password;
}
async saveToDB(): Promis<void>{ // This method is refrenced inside User Interface
const model: ModelType<UserType1> = getModelForClass(UserType1);
await model.create(this);
}
<Other methods inside class>
}
然后我像这样使用上面的代码序列:
const u: User = new UserType1("username", "password");
u.saveToDB();
然后,在 UserType1 集合中保存一条新记录, 但它是空的。 _username、_password 或其他属性都没有保存在记录中。里面只有 _id 和一个名为“__v”的变量(我不知道它是从哪里来的。
【问题讨论】:
-
您使用的是未维护的typegoose版本,请升级到更高版本(目前是最新的7.3.1)
-
将我的 typegoose 从 7.3.0 更新到 7.3.1,没有任何改变。
-
对不起,我猜这是一个未维护的版本,因为这个例子使用了很多旧的东西,比如
extends Typegoose,它在 6.0 中被弃用并在 7.0 中被删除,并尝试使用构造函数,它永远不会被执行在 typegoose 中(如果你使用模型) -
谢谢,我不知道扩展 Typegoose 已被弃用,能否请您指出最新的 Typegoose 文档?因为我在github看到了这种用法,觉得是对的做法。 ps:构造函数永远不会自动执行,我用它来创建一个新对象并将值注入到类字段中。
标签: node.js typescript mongoose typegoose