From b29991e0ff557851392998a78979cbf9ab6031d4 Mon Sep 17 00:00:00 2001 From: Gerard Banasig Date: Wed, 11 Oct 2017 14:33:25 +0800 Subject: [PATCH] Proposal to bring back the access another service from inside a hook A common scenario is we need to access another service from inside the hook, I propose to revive a legacy documentation found on this issue https://github.com/feathersjs/feathers/issues/382 --- api/hooks.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/api/hooks.md b/api/hooks.md index b63db273..c6963ab2 100644 --- a/api/hooks.md +++ b/api/hooks.md @@ -245,3 +245,28 @@ app.hooks({ } }); ``` + +## Access another service from inside a hook + +To access a feathers service from inside a hook using ```hook.app.service('servicename')``` + +The following example we access the ```users``` service from within the message service hook + +```js +app.service('messages').hooks({ + after: { + create: [ + function(hook) { + // We access another service called hook.app.service('users') and fun the create function + return hook.app.service('users').create(hook.data,{}).then(user => { + // Update the result (the message) + hook.result.user = user; + + // Returning will resolve the promise with the `hook` object + return hook; + }); + } + ] + } +}); +```