Search code examples
node.jsfeathersjs

Feathersjs filter results


I have created a node/feathers project using this chat application guide as a base. It's working great, but now I would like to filter the results the api is giving. For example, when user makes GET request to /messages I would like the response to include only the messages that the authorized user has created, not anyone else's messages. Auth is working correctly in the api and message items have the userId who created the message, but I just don't understand what and where I'm supposed to do to filter the messages according to the user id. After hours of googling I couldn't find anything related to this or anyone even asking the question, so what am I missing here?


Solution

  • You can do a manual filtering. Both on before and after hooks. How to use hooks.

    In before hooks you can create a function that update your query object to only get/find data it owns.

    hook.params.query = { ... , ownedBy: hook.params.user._id }
    

    Or do result filtering in after hooks, you have the hook.result which is the only thing you can manipulate in the after hooks. Then you can use Array.prototype.filter() to filter the results the user gets.