Sails.js calls one controller action from another and passes additional parameters to req.body

I need to call a controller action from another action. Until now, I did not need to pass any additional parameters, and the call

sails.controllers.modelName.actionName (req, res)

does the job. However, this time I need to add a new parameter for req.body to call the controller. Is there a way to do this in sails?

I tried the trick given here , but the problem is the changes I make in the activity are lost when I call the action of another controller

EDIT: It works if I try to access the parameter using req.body in another controller action, but don't try to access it using req.params.all ()

+3


source to share


1 answer


This is not a good practice for writing reusable code in Sails. If you want to name one controller for another, you must move the generic code to service and call the service method from both controllers.



In any case, Sails does not allow you to directly change request parameters. You can change req.body

, but really the only thing you have to change in the query is req.options

which was designed for this purpose. This way, you maintain an accurate picture of what was actually received in the request.

+4


source







All Articles