I need to call a controller action from another action. So far, I have not been required to pass any additional params and the call
sails.controllers.modelName.actionName(req, res)
does the job. However, this time I need to add new param to req.body for the controller action to be called. Is there any way to do that in sails ?
I did try the trick specified here but the problem is that the changes I make in the action are lost when I call the other controller action
EDIT: It's working if I try to access the param using req.body in the other controller action but not if I try to access it using req.params.all()
This is not the best practice for writing re-usable code in Sails. If you find yourself wanting to call one controller from another, you should move the shared code into a service and call the service method from both controllers.
In any case, Sails doesn't allow you to modify request params directly. You can modify req.body
, but really the only thing you should modify in the request is req.options
, which was designed for that purpose. This way you keep an accurate representation of what was actually received in the request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With