【发布时间】:2018-10-28 14:32:09
【问题描述】:
我在 es6 中尝试了静态方法,知道为什么我不能像下面这样链接我的静态方法吗?甚至可以链接 2 个静态方法吗?
//nameModel.js
const schema = new mongoose.Schema({ name: String })
class NameClass {
static async findAll() {
return this.find({})
}
}
schema.loadClass(NameClass)
export const model = initModel('NameSchema', schema)
//controller.js
import { model as NameModel } from '../models/nameModel'
export default () => async (req, res) {
try {
const test = await NameModel.findAll()
console.log('test', test) //have all the records
const response = await NameModel.findAll().sort('-name') // NameMode.sort is not a function
} catch (e) {
console.log(e)
}
}
猫鼬模式中的静态和非静态方法有什么区别?我很困惑,因为文档只显示代码示例。我觉得这是多余的,因为它没有显示两个 http://mongoosejs.com/docs/advanced_schemas.html 之间的区别
【问题讨论】:
标签: javascript node.js mongodb mongoose