开发者

Change url after I updated DB

开发者 https://www.devze.com 2023-04-01 03:19 出处:网络
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 ?

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);
                })
            }
        })
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消