I have 2 different methods that I want to be called when a specific form is filled. I know that I can't have a form with 2 actions so I am just wondering can I call 2 different methods on the same route in Node.js?
在填写特定表单时,我想要调用两种不同的方法。我知道我不能有一个带有2个动作的表单所以我只是想知道我可以在Node.js中的同一路径上调用2种不同的方法吗?
I need something like this
我需要这样的东西
router.post('/webmark/addcollection', webmarks.addCollection);
router.post('/webmark/addcollection', webmarks.uploadPicture);
so basically when the button in the form is pressed, the action would redirect to the specific route and the 2 methods would be called.
因此,当按下表单中的按钮时,操作将重定向到特定路径,并且将调用2个方法。
3 个解决方案
#1
0
You can just put the uploadPicture
inside the addCollection
and it will work as you want.
您可以将uploadPicture放在addCollection中,它可以根据需要使用。
#2
2
No, if do it that way, then you will be overwriting the first.
不,如果这样做,那么你将覆盖第一个。
A better approach to that is like below:
更好的方法如下:
router.post('/webmark/addcollection', webmarks.addCollection, webmarks.uploadPicture);
And make sure you make the call to next middleware function here uploadPicture
from addCollection
handler by adding next()
in addCollection
middleware on the successful operation.
并确保通过在成功操作的addCollection中间件中添加next(),从addCollection处理程序中调用下一个中间件函数uploadPicture。
exports.addCollection = function(req, res, next){
// You logic goes here
// On success operation call next middleware
next();
}
exports.uploadPicture = function(req, res){
// You logic for uploadPicture
}
#3
0
Your first function receives 3 input (request, response, next)
, at the end of this function, call next()
.
您的第一个函数接收3个输入(请求,响应,下一个),在此函数结束时,调用next()。
#1
0
You can just put the uploadPicture
inside the addCollection
and it will work as you want.
您可以将uploadPicture放在addCollection中,它可以根据需要使用。
#2
2
No, if do it that way, then you will be overwriting the first.
不,如果这样做,那么你将覆盖第一个。
A better approach to that is like below:
更好的方法如下:
router.post('/webmark/addcollection', webmarks.addCollection, webmarks.uploadPicture);
And make sure you make the call to next middleware function here uploadPicture
from addCollection
handler by adding next()
in addCollection
middleware on the successful operation.
并确保通过在成功操作的addCollection中间件中添加next(),从addCollection处理程序中调用下一个中间件函数uploadPicture。
exports.addCollection = function(req, res, next){
// You logic goes here
// On success operation call next middleware
next();
}
exports.uploadPicture = function(req, res){
// You logic for uploadPicture
}
#3
0
Your first function receives 3 input (request, response, next)
, at the end of this function, call next()
.
您的第一个函数接收3个输入(请求,响应,下一个),在此函数结束时,调用next()。