简体   繁体   English

猫鼬模型,为什么我需要使用“ new”? (TypeScript)

[英]mongoose model, why do I need to use “new”? (TypeScript)

I don't understand this completely, as you will see. 如您所见,我对此并不完全理解。

But I create a new mongoose.model like this: 但是我创建一个新的mongoose.model像这样:

let MyModel = moongoose.model<IMyModel>("myModel", MyModelSchema);

What is the diffrence on MyModel and let newModel = new MyModel ? MyModellet newModel = new MyModel什么?

I need to create newModel to use function such as .save() . 我需要创建newModel才能使用.save() Why is that? 这是为什么?

当使用moongoose.model ,您不是在创建模型的实例,而是在为其创建构造函数-换句话说, MyModel是一个类,而newModel是该类的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM