1

I have self contained model in invoice.js

'use strict';

// load the things we need
var mongoose = require('mongoose');
var auth_filter = require('../../auth/acl/lib/queryhook');
var invoice_db = mongoose.createConnection(config.mongo.url + '/invoiceDB');

// PROMISE LIBRARY USED FOR ASYNC FLOW
var promise  = require("bluebird");

var Schema     = mongoose.Schema, ObjectId = Schema.Types.ObjectId;

// define the schema for our invoice details model
var invoicedetailSchema = new Schema({
    //SCHEMA INFO
});
var InvoiceModel = invoice_db.model('InvoiceDetail', invoicedetailSchema);
// create the model for seller and expose it to our app
auth_filter.registerHooks(InvoiceModel);


module.exports = InvoiceModel;

I want to hook to the pre query for this model. I am trying to accomplish that using hooks but i am not successful with that. I am registering the hook using auth_filter file as below

'use strict';

var hooks = require('hooks'),
    _ = require('lodash');


exports.registerHooks = function (model) {


model.pre('find', function(next,query) {
      console.log('test find');
      next();
   });

model.pre('query', function(next,query) {
      console.log('test query');
      next();
   });

};

What am I doing wrong? I want to keep the hooks separate so that I can call for a lot of different models.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
Uma Maheshwaraa
  • 563
  • 2
  • 9
  • 17

1 Answers1

1

Query hooks need to be defined on the schema, not the model. Also, there is no 'query' hook, and the query object is passed to the hook callback as this instead of as a parameter.

So change registerHooks to be:

exports.registerHooks = function (schema) {

    schema.pre('find', function(next) {
          var query = this;
          console.log('test find');
          next();
       });
};

And then call it with the schema before creating your model:

var invoicedetailSchema = new Schema({
    //SCHEMA INFO
});

auth_filter.registerHooks(invoicedetailSchema);

var InvoiceModel = invoice_db.model('InvoiceDetail', invoicedetailSchema);
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
  • Thanks for the clarification. This works. Meanwhile I looked at a library called hooks it requires a query object such as mongoose.query to hook. Is there a way to get a query object from a mongoose model and use hooks. I find it much more powerful to do that way. The implementation is straightforward if there is only one model as I am able to hook to mongoose.query. With multiple models it leads to circular reference – Uma Maheshwaraa Jul 25 '15 at 21:30
  • when i try to hook the pre query, it is not firing. I am calling the model using Invoice.find({}).exec()... – Uma Maheshwaraa Jul 25 '15 at 22:16
  • 1
    @UmaMaheshwaraa There is no `'query'` hook, only `'find'` and `'findOne'`. Also, the query object is passed to the hook callback as `this` instead of a parameter. See the updated answer. – JohnnyHK Jul 26 '15 at 00:38
  • thanks for the feedback. i figured that. But i need to hook it at the model level because i need to know the model name for another function at the 'pre' hook of the find. Using the schema i cannot do so. [link](http://stackoverflow.com/questions/31633286/what-is-the-best-way-to-hook-to-existing-mongoose-model) If you have any solution please help – Uma Maheshwaraa Jul 26 '15 at 03:15
  • @JohnnyHK Is it possible to create a new hook after creating the model? Elsewhere in the doc using mongoose.model ('MyModel'). Schema – Legna Oct 07 '21 at 21:33