Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js calling one controller action from another and passing additional params in req.body

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()

like image 825
Mandeep Singh Avatar asked Aug 07 '14 13:08

Mandeep Singh


1 Answers

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.

like image 73
sgress454 Avatar answered Nov 03 '22 21:11

sgress454