Sails.js从另一个调用一个控制器动作并在req.body中传递其他参数 [英] Sails.js calling one controller action from another and passing additional params in req.body

查看:73
本文介绍了Sails.js从另一个调用一个控制器动作并在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)

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

完成任务.但是,这一次我需要向req.body添加新的参数,以调用控制器动作.有办法做到这一点吗?

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

编辑:如果我尝试在其他控制器操作中使用req.body访问该参数,但如果我尝试使用req.params.all()则无法访问

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

推荐答案

这不是在Sails中编写可重用代码的最佳实践.如果您发现自己想从另一个控制器中调用一个控制器,则应将共享代码移至服务 service ,然后从两个控制器调用服务方法.

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.

无论如何,Sails不允许您直接修改请求参数.您可以修改req.body,但是实际上您应该在请求中唯一修改的是req.options,它是为此目的而设计的.这样,您就可以准确表示请求中实际收到的内容.

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.

这篇关于Sails.js从另一个调用一个控制器动作并在req.body中传递其他参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆