简体   繁体   中英

Nodejs page not redirecting after POST

My problem is similar enough to this but also different and the answers aren't working for me.

I have a form to post and I need to display a message on the page whether it entered the data to the database successfully or not. So I have:

app.post('/add_new', upload.single('image'), function add_new(req, res, err)

{
   var image = req.body.image; 
   // do some checks and try to insert
    if (err){
        res.render('page_name',{images:images, message:'There was an error'})
    }
    else{
     images.push(image);
     res.render('page_name',{images:images, message: 'Success'})}
    }
}

The error/success messages are handled on the client side.

My problem is that it doesn't actually complete the post. It renders the page with the error/success message correctly but the URL looks like 'my_site.com/add_new', and if the user refreshes the page for any reason, it sends that 'form resubmission' alert, which causes problems. I need it to go back to 'my_site.com' and display the message.

I have found that doing

res.redirect('back') 

returns to the correct page but obviously doesn't display the message. I have noticed a few answers recommending

res.end('Success') 

but that just displays a white page with the word 'Success' - I don't know how anyone thinks that's an acceptable solution!

EDIT: Following Vinay's answer, my code now reads like:

app.post('/add_new', upload.single('image'), function add_new(req, res, err)

{
   var image = req.body.image; 
   // do some checks and try to insert
    if (err){
        req.session.msg='error adding';
        res.redirect('page_name');
    }
    else{
     images.push(image);
     req.session.msg='success';
     res.redirect('page_name');
    }
}

and this to get the page:

app.get('/page_name', is_logged_in, function(req, res) {
    if(req.session.msg){
        res.render('page_name', {images: images, user:req.user, message: req.session.msg});
        setTimeout(req.session.msg="", 4000);
    }
    else{
        res.render('page_name', {images: images, user:req.user, message:''});
    }
});

But I am still finding that the error message persists when the page is refreshed. Have I made a mistake?

currently you are doing this

if (err){
    res.render('page_name',{images:images, message:'There was an error'})
}
else{
 images.push(image);
 res.render('page_name',{images:images, message: 'Success'})}
}

you are getting url like this to 'my_site.com/add_new', which is correct rendering means your are loading/rendering html page to particular route which is '/add_new'.

There may be a two solution

1st solution

res.redirect('/?msg=' + string);

and on home page

app.get('/', function(req, res){
  if(req.query.msg){
      // display error message
  }else{
      // else do not display error
   }
});

2nd solution

req.session.msg = "message here";
res.redirect('/');

and on home page you can do like this.

if(req.session.msg){ 
    //display error message here
    // after displaying msg in that last this 
    setTimeout(req.session.msg="", 3000); // this will clear a session after 3 seconds, so next time it will not display the msg.
  } else {
    //or display home page without error.
  };

you can use one of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM