如何在ES6 / ES2015中编写一个Mongoose模型

时间:2016-01-01 21:27:44

标签: javascript node.js mongoose ecmascript-6 es2015

我想在ES6中编写我的猫鼬模型。基本上尽可能地替换module.exports和其他ES5的东西。这就是我所拥有的。

import mongoose from 'mongoose'

class Blacklist extends mongoose.Schema {
  constructor() {
    super({
      type: String,
      ip: String,
      details: String,
      reason: String
    })
  }
}

export default mongoose.model('Blacklist', Blacklist)

我在控制台中看到此错误。

if (!('pluralization' in schema.options)) schema.options.pluralization = this.options.pluralization;
                                 ^

TypeError: Cannot use 'in' operator to search for 'pluralization' in undefined

7 个答案:

答案 0 :(得分:14)

我不确定为什么你在这种情况下尝试使用ES6课程。 mongoose.Schema是用于创建新架构的构造函数。当你这样做

var Blacklist = mongoose.Schema({});

您正在使用该构造函数创建新架构。构造函数的设计使其行为与

完全相同
var Blacklist = new mongoose.Schema({});

你可以选择什么,

class Blacklist extends mongoose.Schema {

确实创建了一个schema类的子类,但你实际上从未在任何地方实例化它

你需要做

export default mongoose.model('Blacklist', new Blacklist());

但我不会真的推荐它。什么都没有"更多ES6y"关于你在做什么。以前的代码非常合理,是Mongoose的推荐API。

答案 1 :(得分:7)

你为什么要这样做?预计不会以这种方式使用mongoose.Schema。它不使用继承。

mongoose.Schema是一个构造函数,它将对象作为ES5和ES6中的第一个参数。这里不需要ES6课程。

因此即使使用ES6,正确的方法是:

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

答案 2 :(得分:5)

要按照类似于类的方式执行ES6,如问题所述,我只需在导出的ObservableRangeCollection<TheClass> ob2 = new ObservableRangeCollection<TheClass>(); ob2.AddRange(ob1.Take(3));//Returns 3 number of contiguous elements from the start // Or to grab a Range between 2 and 5 ob2.AddRange(ob1.Skip(2).Take(3)) 函数中使用new调用该类。

mongoose.model

答案 3 :(得分:4)

对于那些发现这种搜索的人来说,原始问题似乎对我来说非常有用。我使用Babel将ES6 +转换为5.我的自定义mongoose方法与我的调用类中的异步/等待代码不兼容。值得注意的是this在我的实例方法中是null。使用此处提供的解决方案,我能够得到这个解决方案,希望能帮助其他人搜索。

import mongoose from 'mongoose'

class Tenant extends mongoose.Schema {
  constructor() {
    const tenant = super({
      pg_id: Number,
      name: String,
      ...
    })

    tenant.methods.getAccountFields = this.getAccountFields
    tenant.methods.getCustomerTypes = this.getCustomerTypes
    tenant.methods.getContactFields = this.getContactFields
    ...
    tenant.methods.getModelFields = this.getModelFields

    return tenant
  }

  getAccountFields() {
    return this.getModelFields(this.account_fields_mapping)
  }

  getCustomerTypes() {
    //code
  }

  getContactFields() {
    //code
  }

  ...

  getModelFields(fields_mapping) {
    //code
  }
}

export default mongoose.model('Tenant', new Tenant)

答案 4 :(得分:4)

Mongoose can natively support es6 classes(自4.7以来,没有转发......)。

只需写下:

const mongoose = require('mongoose')
const { Model, Schema } = mongoose

const schema = new Schema({
  type: String,
  ip: String,
  details: String,
  reason: String,
})

class Tenant extends Model {}

module.exports = mongoose.model(Tenant, schema, 'tenant');

答案 5 :(得分:1)

这可能会延迟回复,这可能会对寻求帮助的人有所帮助。

对于ES6类,模式具有loadClass()方法,可用于从ES6类创建Mongoose模式:

  • ES6类方法成为猫鼬方法
  • ES6类静态成为猫鼬静态
  • ES6吸气剂和吸气剂成为猫鼬虚拟动物

这是一个使用loadClass()从ES6类创建模式的示例:

class MyClass {
  myMethod() { return 42; }
  static myStatic() { return 42; }
  get myVirtual() { return 42; }
}

const schema = new mongoose.Schema();
schema.loadClass(MyClass);

console.log(schema.methods); // { myMethod: [Function: myMethod] }
console.log(schema.statics); // { myStatic: [Function: myStatic] }
console.log(schema.virtuals); // { myVirtual: VirtualType { ... } }

参考:这是猫鼬文档中的示例代码,更多信息mongoose doc

答案 6 :(得分:0)

这将起作用:

import mongoose from 'mongoose';

const { Schema } = mongoose;
const userSchema = new Schema({

  email: {
      type: String,
      required: true
  },
  firstName: {
      type: String,
  },
  lastName: {
      type: String
  },
  age: {
      type: Number
  }
});

export default mongoose.model('User', userSchema);