Javascript 更新数据库后更改url
当我在数据库中更新了一篇文章的标题后,我想重定向到一个页面,其中第二个uri将是新标题而不是旧标题…在express js中如何做到这一点Javascript 更新数据库后更改url,javascript,node.js,express,Javascript,Node.js,Express,当我在数据库中更新了一篇文章的标题后,我想重定向到一个页面,其中第二个uri将是新标题而不是旧标题…在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 : old
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.render('blog_update' , {locals:{title:'Updated Successfully!', post: arr }});
})
}
})
}
});
基于newTitle
构造一个新的uri,然后调用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);
})
}
})
}
});
谢谢,它只是给了我一个网址说:未定义。重定向到,然后我必须单击…我是否也必须渲染页面才能自动执行此操作?@rabidmachine9您是否解决过此问题以使其自动运行?
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);
})
}
})
}
});