如何从钩子访问猫鼬模型
How to access to mongoose models from a hook
我正在尝试直接访问使用 feathers CLI 创建的模型。在 feathers-mongoose 文档中指出:
注意:您可以通过钩子内的 this.Model 访问 Mongoose 模型并照常使用它。有关定义模型的更多信息,请参阅 Mongoose 指南。
我试过像这样从自定义挂钩访问模型:
module.exports = function (options = {}) {
return async context => {
this.Model
return context
}
}
我期待模型回来,但我只是得到 undefined
。
首先是尽量不要使用箭头函数
return async function(context) => {
...
}
我建议通过上下文访问它context.service('serviceName').Model'
我正在尝试直接访问使用 feathers CLI 创建的模型。在 feathers-mongoose 文档中指出:
注意:您可以通过钩子内的 this.Model 访问 Mongoose 模型并照常使用它。有关定义模型的更多信息,请参阅 Mongoose 指南。
我试过像这样从自定义挂钩访问模型:
module.exports = function (options = {}) {
return async context => {
this.Model
return context
}
}
我期待模型回来,但我只是得到 undefined
。
首先是尽量不要使用箭头函数
return async function(context) => {
...
}
我建议通过上下文访问它context.service('serviceName').Model'