2011-09-01 54 views
0

在我更新了我的數據庫中的帖子標題後,我想重定向到第二個uri將成爲新標題的頁面,而不是舊的...我怎麼能在表達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{ 
    posts.findOne({title : req.body.post.title}, function(err, arr){ 
     if(err) throw err 

     res.render('blog_update' , {locals:{title:'Updated Successfully!', post: arr }}); 
    }) 
     } 
    }) 
    } 
}); 

回答

0
res.redirect("/blog_update/" + newTitle); 

建設基礎上,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); 
       }) 
      } 
     }) 
    } 
}); 
+1

感謝它只是給了我一個網址,說:不確定。重定向到http:// localhost:8080/blog_update/new_title,然後我必須點擊...我是否也必須呈現一個頁面才能自動執行此操作? – rabidmachine9

+0

@ rabidmachine9你曾經解決過這個問題嗎? –