简体   繁体   中英

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:

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

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

I need to create newModel to use function such as .save() . Why is that?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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