How to write a Mongoose model in ES6 / ES2015

前端 未结 7 2157
自闭症患者
自闭症患者 2021-02-18 23:55

I want to write my mongoose model in ES6. Basically replace module.exports and other ES5 things wherever possible. Here is what I have.

import mongo         


        
7条回答
  •  萌比男神i
    2021-02-19 00:14

    Why would you want to do it? mongoose.Schema is not expected to be used in this way. It doesn't use inheritance.

    mongoose.Schema is a constructor that takes an object as the first parameter both in ES5 and ES6. No need for ES6 classes here.

    Thus even with ES6 the proper way is to have:

    const Blacklist = mongoose.Schema({
      type: String,
      ip: String,
      details: String,
      reason: String,
    });
    

提交回复
热议问题