reactjs - Redirecting to route from Node.JS server doesn't render the next page -


hi have react component renders form this:

<form onsubmit={this.onsubmit}>   <!-- bunch of inputs here --> </form> 

where function onsubmit() makes post request /results using axios:

handlesubmit(e) {     var self = this;     e.preventdefault();     const {value1, value 2, ....} = this.state;     axios.post('/results', {         key1 : value1,         key2 : value2,         etc.     }).then(function(response) {       if (errors) {           self.setstate({errors: response.data.errormessage});       }      }).catch(function(error){       console.log(error);     });   } 

i have route handler post request in server.js inserts data form database. if there errors it'll send data client, otherwise should redirect results page. handler looks this:

app.post('/results', function(req, res, next) {   const reportexists = report.findone({     attributes: ['caseid'],     where: {caseid : req.body.caseid},   }).then(report => {     if (report) {       console.log("report exists");       res.status(200).send({errormessage : "report has been submitted case id"});     } else {       const report = report.create(         {           // data form         }       ).then(() => {         console.log('record inserted successfully');         var caseid = req.body.caseid;         res.redirect("/results/" + caseid);         next();       })       .catch(err => {         console.log('failed insert record');         res.status(200).send({errormessage: "failed insert record"});       });     }   }); }); 

i have app.get('/results/:caseid') handler should render appropriate route results page. when record inserted doesn't redirect page, stays on same page form. question is, should redirecting page client or server?

submitting ajax call via client-side javascript gets response server, whatever is. redirect not automatically processed browser, it's ajax response javascript. client-side javascript decide redirect response.

you have couple choices. detect redirect response in client-side javascript , set window.location new location , manually tell browser go new page. or, let browser submit form rather client-side javascript , browser follow redirect response automatically.


also, should not calling next() after call res.redirect(). once you've sent response, should not enable other route handlers execute.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -