5
在我的模型中,我試圖做一個靜態的getUserByToken方法。但是,如果我不喜歡它的文檔中,我得到Mongoose - this.find()不存在
this.find is not a function
我的代碼如下所示:
'use strict';
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const schema = new Schema({
mail: {
type: String,
required: true,
validate: {
validator: (mail) => {
return /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i.test(mail);
}
}
},
birthDate: {
type: Date,
required: true,
max: Date.now,
min: new Date('1896-06-30')
},
password: {
type: String,
required: true
},
...
});
schema.statics.getUserByToken = (token, cb) => {
return this.find({ examplefield: token }, cb);
};
module.exports.Schema = schema;
我猜測這只是一個簡單的錯誤,但是,我不能編譯該模型並將靜態函數添加到模式/模型,因爲這是通過啓動時的init函數完成的,該模塊編譯所有模型。
任何人都可以幫助我嗎?