Cal*_*ein 3 virtual mongoose node.js
我想做的是这样的:
Schema
.virtual('getSomething')
.get(function(what) {
if (!what) {
return this.somethingElse
} else {
return this.something[what]
}
})
Run Code Online (Sandbox Code Playgroud)
问题是我们不能在虚拟getter中传递参数,如何在不重复代码的情况下实现类似的东西呢?
将其添加为实例方法而不是虚拟getter.
schema.methods.getSomething = function(what) {
if (!what) {
return this.somethingElse
} else {
return this.something[what]
}
};
Run Code Online (Sandbox Code Playgroud)