After I have updated the title of a post in my DB I want to redirect to a page where the second uri would be the new title instead of the old...how can I do that in express js ?
app.post('/blog_update/:title', function(req, res){
var oldTitle = req.params.title
var newTitle = req.body.post.title
if(req.body.post.submit){
posts.update({title : oldTitle}, {
title : req.body.post.title,
body : req.body.post.body,
tags : req.body.post.tags
}, function(err){
if(err) throw err;
else{
开发者_如何学Go posts.findOne({title : req.body.post.title}, function(err, arr){
if(err) throw err
res.render('blog_update' , {locals:{title:'Updated Successfully!', post: arr }});
})
}
})
}
});
res.redirect("/blog_update/" + newTitle);
Construct a new uri based on the newTitle
then call res.redirect
app.post('/blog_update/:title', function(req, res) {
var oldTitle = req.params.title
var newTitle = req.body.post.title
if (req.body.post.submit) {
posts.update({
title: oldTitle
}, {
title: req.body.post.title,
body: req.body.post.body,
tags: req.body.post.tags
}, function(err) {
if (err) throw err;
else {
posts.findOne({
title: req.body.post.title
}, function(err, arr) {
if (err) throw err
res.redirect("/blog_update/" + newTitle);
})
}
})
}
});
精彩评论