html - Run multiple functions based on a SINGLE form submission (method="post") using Node-express -
i looking perform multiple actions upon receiving html(or ejs) form content using post method. using node express, mongoose & mongodb. each of below post responses work individually unsure how proceed in updating multiple databases based on 1 single form submission.
// insert passport db app.post('/signup', passport.authenticate('local-signup', { successredirect : '/index', // redirect secure profile section failureredirect : '/signup', // redirect signup page if there error failureflash : true // allow flash messages })); //insert database here [the content of in second function unimportant working fine , has been stripped down simplification.]
app.post('/signup', function( req, res ) { new userdb( { user_id : req.body.content, first_name : req.body.fname, }).save( function( err, mysite, count ) { res.redirect( '/index' ); }); }); i have tried redirecting form content not accessible after redirect first function stores data (ie. 1 database filled).
how run both functions within
app.post('/signup',..... { ... }); ?
thanks in advance!
you can making 1 function callback of other. easy because each function maintains same connect middleware signature, function(req, res, next), req , res request , response objects created , manipulated application, , next next function call @ end of current function's execution.
according the official documentation, passport.authenticate() normal piece of middleware. need specify middleware want called next. express queues middleware functions in order in pass them app.post. can this:
app.post('/signup', passport.authenticate('local-signup', { failureredirect : '/signup', failureflash : true }), function(req, res) { new userdb({ user_id : req.body.content, first_name : req.body.fname, }).save(function(err, mysite, count) { res.redirect('/index'); }); }); middleware extremely powerful feature of express framework , possibly single important 1 master. this guide great next step if want learn more.
Comments
Post a Comment