0

I'm following the tutorial. In Asynchronous hooks, there's a snippet like this:

todoService.before({
  find(hook) {
    return this.find().then(data => {
      hook.params.message = 'Ran through promise hook';
      hook.data.result = data;
      // Always return the hook object
      return hook;
    });
  }
});

Would you please let me know what this.find() is supposed to do?

anhldbk
  • 4,559
  • 5
  • 29
  • 37

1 Answers1

3

find is a Feathers service method and this is the service the hook is running on.

Daff
  • 43,734
  • 9
  • 106
  • 120
  • So the flow will be: 1. The hook will call the service to modify associated data 2. The service is invoked on the modified data Is that right? – anhldbk May 11 '16 at 09:49
  • Ah, I see the problem, `hook.data.result = data;` is not very useful here. I took it out of the documentation. Basically it calls the services `find` method. Then you can do something with that data and e.g. add some information with `hook.params.message` which will then be available in the following hooks and the service method call `params`. `before` hooks let you modify the query, parameters and user submitted data (e.g. validation) where `after` hooks let you modify the returned data. – Daff May 11 '16 at 15:53
  • Well, I think we should have a `better demo` here! Instead of letting `find hook` calls `find service` (and then `find service` is executed once again), we should do things like: `hook.app.service('messages').find().then()`. I think it's much clearer to understand the code. – anhldbk May 11 '16 at 17:11
  • 1
    Makes sense. You are welcome to [change it here](https://github.com/feathersjs/feathers-docs/edit/master/hooks/usage.md) – Daff May 11 '16 at 22:56