Search code examples
feathersjsfeathers-hook

What is context.dispatch in feathers.js


I read the feathersjs API documentation. (this)
But I can't understand what is the context.dispatch. and what does this do.

context.dispatch is a writeable, optional property and contains a "safe" version of the data that should be sent to any client. If context.dispatch has not been set context.result will be sent to the client instead.

What is the "safe" version means?
When will I want to use it?


Solution

  • The "safe" version is what a client can see and is specifically important for hiding fields - like the password - in real-time events. For example, if - on the server - you do:

    app.service('users').patch(user.id, { enabled: false })
    

    The return value will include the hashed password even though it is using the protect hook because it is an internal call.

    However, context.dispatch will not include the password and the patched real-time event will only send back the safe data to the client.