I want to write my mongoose model in ES6. Basically replace module.exports
and other ES5 things wherever possible. Here is what I have.
我想用ES6写我的猫鼬模型。基本上取代模块。尽可能出口和其他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)
I see this error in the console.
我在控制台上看到这个错误。
if (!('pluralization' in schema.options)) schema.options.pluralization = this.options.pluralization;
^
TypeError: Cannot use 'in' operator to search for 'pluralization' in undefined
5 个解决方案
#1
12
I'm not sure why you're attempting to use ES6 classes in this case. mongoose.Schema
is a constructor to create new schemas. When you do
我不确定为什么要在这种情况下使用ES6类。猫鼬。模式是创建新模式的构造函数。当你做
var Blacklist = mongoose.Schema({});
you are creating a new schema using that constructor. The constructor is designed so that behaves exactly like
您正在使用该构造函数创建一个新模式。构造函数被设计成完全相同的行为
var Blacklist = new mongoose.Schema({});
What you're alternative,
你的选择,
class Blacklist extends mongoose.Schema {
does is create a subclass of the schema class, but you never actually instantiate it anywhere
是否创建了模式类的子类,但是从来没有实例化过它
You'd need to do
你需要做的
export default mongoose.model('Blacklist', new Blacklist());
but I wouldn't really recommend it. There's nothing "more ES6y" about what you are doing. The previous code is perfectly reasonable and is the recommended API for Mongoose.
但我不会推荐它。你正在做的事情没有什么比这更重要的了。前面的代码非常合理,是Mongoose的推荐API。
#2
7
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.
猫鼬。Schema是一个构造函数,它将对象作为ES5和ES6中的第一个参数。这里不需要ES6类。
Thus even with ES6 the proper way is to have:
因此,即使使用ES6,正确的方法是:
const Blacklist = mongoose.Schema({
type: String,
ip: String,
details: String,
reason: String,
});
#3
5
To do things the ES6, class-like way, as the question states, I simply had to invoke the class with new
in the exported mongoose.model
function.
如问题所述,要以类类的方式处理ES6,我只需在导出的mongoose中调用带有new的类。模型的功能。
export default mongoose.model('Blacklist', new Blacklist)
#4
3
For those who find this searching around, the original question seems pretty valid to me. I'm using Babel transpiling ES6+ down to 5. My custom mongoose methods did not play well with my async/await code in my calling class. Notably this
was null
in my instance methods. Using the solution provided here, I was able to arrive at this solution that hopefully helps others searching around.
对于那些四处寻找的人来说,最初的问题对我来说似乎很有道理。我用的是通心粉把ES6+减到5。我的自定义mongoose方法在我的调用类中不适合我的异步/等待代码。值得注意的是,在我的实例方法中这是空的。使用这里提供的解决方案,我能够得到这个有望帮助其他人搜索的解决方案。
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)
#5
3
Mongoose can natively support es6 classes (since 4.7, and with no transpiler…).
Mongoose可以在本地支持es6类(从4.7开始,没有传送器…)。
Just write:
只写:
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');
#1
12
I'm not sure why you're attempting to use ES6 classes in this case. mongoose.Schema
is a constructor to create new schemas. When you do
我不确定为什么要在这种情况下使用ES6类。猫鼬。模式是创建新模式的构造函数。当你做
var Blacklist = mongoose.Schema({});
you are creating a new schema using that constructor. The constructor is designed so that behaves exactly like
您正在使用该构造函数创建一个新模式。构造函数被设计成完全相同的行为
var Blacklist = new mongoose.Schema({});
What you're alternative,
你的选择,
class Blacklist extends mongoose.Schema {
does is create a subclass of the schema class, but you never actually instantiate it anywhere
是否创建了模式类的子类,但是从来没有实例化过它
You'd need to do
你需要做的
export default mongoose.model('Blacklist', new Blacklist());
but I wouldn't really recommend it. There's nothing "more ES6y" about what you are doing. The previous code is perfectly reasonable and is the recommended API for Mongoose.
但我不会推荐它。你正在做的事情没有什么比这更重要的了。前面的代码非常合理,是Mongoose的推荐API。
#2
7
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.
猫鼬。Schema是一个构造函数,它将对象作为ES5和ES6中的第一个参数。这里不需要ES6类。
Thus even with ES6 the proper way is to have:
因此,即使使用ES6,正确的方法是:
const Blacklist = mongoose.Schema({
type: String,
ip: String,
details: String,
reason: String,
});
#3
5
To do things the ES6, class-like way, as the question states, I simply had to invoke the class with new
in the exported mongoose.model
function.
如问题所述,要以类类的方式处理ES6,我只需在导出的mongoose中调用带有new的类。模型的功能。
export default mongoose.model('Blacklist', new Blacklist)
#4
3
For those who find this searching around, the original question seems pretty valid to me. I'm using Babel transpiling ES6+ down to 5. My custom mongoose methods did not play well with my async/await code in my calling class. Notably this
was null
in my instance methods. Using the solution provided here, I was able to arrive at this solution that hopefully helps others searching around.
对于那些四处寻找的人来说,最初的问题对我来说似乎很有道理。我用的是通心粉把ES6+减到5。我的自定义mongoose方法在我的调用类中不适合我的异步/等待代码。值得注意的是,在我的实例方法中这是空的。使用这里提供的解决方案,我能够得到这个有望帮助其他人搜索的解决方案。
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)
#5
3
Mongoose can natively support es6 classes (since 4.7, and with no transpiler…).
Mongoose可以在本地支持es6类(从4.7开始,没有传送器…)。
Just write:
只写:
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');