我做错了什么?
我有一个架构
import * as mongoose from 'mongoose';
const PolicySchema = new mongoose.Schema({
number: { type: 'String', required: true },
startDate: { type: mongoose.Schema.Types.Date, required: true },
endDate: { type: mongoose.Schema.Types.Date, required: true },
],
});
PolicySchema.statics.lookup = function() {
console.log(this);
};
export {PolicySchema};
当在我的服务中时,我称之为
@Injectable()
export class PolicyService {
async test() {
this.policyModel.schema.statics.lookup();
}
constructor(@InjectModel('Policy') private readonly policyModel: Model<Policy>) { }
}
在控制台上,我可以读取我的“这个”上下文只是一个 { 查找:[函数]} 我的错误在哪里?
在“This”上,我想获得聚合或获取模式之类的功能。
也许我叫错了?
请帮我 :)
附注。我使用 nest.js
我看了一下文档:https : //mongoosejs.com/docs/guide.html#statics
看起来您可以static
直接在模型上调用函数,尽管您可能必须为包含新函数的模型创建一个接口。
所以,你上面的例子应该读
interface PolicyModel extends Model<Policy> {
lookup: () => void;
}
@Injectable()
export class PolicyService {
async test() {
this.policyModel.lookup();
}
constructor(@InjectModel('Policy') private readonly policyModel: PolicyModel) { }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句