我想用 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
原文由 Noah 发布,翻译遵循 CC BY-SA 4.0 许可协议
我不确定您为什么要在这种情况下尝试使用 ES6 类。
mongoose.Schema
是创建新模式的构造函数。当你做您正在使用该构造函数创建一个新模式。构造函数的设计使其行为完全像
你是另类,
所做的是创建模式类的子类,但您实际上从未在任何地方实例化它
你需要做
但我不会真的推荐它。关于你在做什么,没有什么“更多的 ES6y”。前面的代码非常合理,是Mongoose推荐的API。