Search code examples
node.jsexpressmongoosefeathersjs

How to access to mongoose models from a hook


I'm trying to get direct access to the models that were created with the feathers CLI. In the feathers-mongoose documentation it's stated that:

Note: You can get access to the Mongoose model via this.Model inside a hook and use it as usual. See the Mongoose Guide for more information on defining your model.

I've tried to access the model from a custom hook like this:

module.exports = function (options = {}) {
  return async context => {
    this.Model
    return context
  }
}

I was expecting the model back but I just get undefined.


Solution

  • First is try not to use arrow functions.

    return async function(context) => {
        ...
    }
    

    I suggest accessing it via context context.service('serviceName').Model'