2017-10-06 125 views
0

尝试在删除(或更新)内部使用req.params.id路由时,我收到上述消息。这让我陷入了一段时间,我确信我在我的路线/对象的某个地方犯了一个错误。[CastError:在路径“_id”处投射到ObjectId失败,值为“undefined”

将res.render(“/ campgrounds/+ req.params.id)”应用程序更改为 - res.render(“/ campgrounds”);解决了问题,但不会像我一样重新加载相同的页面“M看看有没有它做的。我不能换我周围为什么访问来自req.params.id营地的路线时,应用程序将返回未定义的头。

var express= require("express"); 
var router = express.Router(); 
var Comment = require("../models/comment"); 
var Campground = require("../models/campgrounds"); 


// COMMENTS EDIT ROUTE 



router.get("/campgrounds/:id/comments/:comment_id/edit", function(req, res){ 

      Comment.findById(req.params.comment_id, function(err, foundComment){ 
       if(err){ 
        console.log(err) 
       } else { 
        res.render("comments/edit", {campground_id: req.params.id, comment: foundComment}) 
       } 
      }) 
     }) 


// comment update 

//campgrounds/:id/comments/:comment_id 
router.put("/:comment_id", function(req, res){ 
    Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, function(err, updatedComment){ 
     if(err){ 
      console.log(err) 
     } else { 
      // KNOWN BUG - /campgrounds/ + req.params.id will return cast to boject failed for value undefined at path _id. having the app redirect to all campgrounds page as a work around 
      res.redirect("/campgrounds"); 
     } 
    }) 
}) 

// DELETE ROUTER 
router.delete("/:comment_id", function(req, res){ 
    Comment.findByIdAndRemove(req.params.comment_id, function(err){ 
     if(err){ 
      res.redirect("back"); 
     } else { 

      res.redirect("/campgrounds/" + req.params.id); 
     } 
    }) 
}) 



function isLoggedIn(req, res, next){ 
    if(req.isAuthenticated()){ 
    return next(); 
    } else { 
     res.redirect("/login"); 
    } 
} 

module.exports = router; 
+0

,你能不能给我请求URL你是如何访问的网址从服务器@ Andy – Vignesh

+0

app.listen(process.env.PORT,process.env.IP,function(){ console.log(“App is running”); })< - 这是你在找什么? @Vignesh – Andy

回答

0

请确保您有输入字段名“ID “在评论的形式(或 ”AJAX请求ID“)。

router.put("/:comment_id", function(req, res){ 
 
    const id = req.params.id; 
 
    console.log(id); 
 
    Comment.findByIdAndUpdate(req.params.comment_id, req.body.comment, function(err, updatedComment){ 
 
     if(err){ 
 
      console.log(err) 
 
     } else { 
 
      res.redirect("/campgrounds/" + id); 
 
     } 
 
    }) 
 
})

+0

我有我的输入字段设置为:

/comments/<%= comment._id%>?_ method = DELETE”method =“POST”>
对不起格式化我有一段时间试图弄清楚如何在堆栈中格式化注释。 – Andy

+0

你是否暗示我应该在名称中加入name =“id”? @ĐứcLêMinh – Andy

+0

我认为你应该在评论中储存campground_id。那么你可以重定向到“campgrounds /”+ updatedComment.campground_id –

0

我觉得你的问题是你是不是从HTML发送COMMENT_ID到控制器尝试打印req.params.comnent_id

那就试试这个

var express= require("express"); 
    var router = express.Router(); 
    var Comment = require("../models/comment"); 
    var Campground = require("../models/campgrounds"); 


    // COMMENTS EDIT ROUTE 



    router.get("/campgrounds/:id/comments/:comment_id/edit", function(req, res){ 
    console.log("params.comment_id",params.comment_id); 
    if(req.params.comment_id){ 
     Comment.findById(req.params.comment_id, function(err, foundComment){ 
        if(err){ 
         console.log(err) 
        } else { 
         res.render("comments/edit", {campground_id: req.params.id, comment: foundComment}) 
        } 
       }else { 
         res.render("comments/edit", {campground_id: req.params.id, comment: foundComment}) 
        } 

       }) 
      }) 
+0

所以这是来自我设置的edit.ejs文件的html:'

/comments/<% = comment._id%>?_ method = put“method =”POST“>'我已经将您的代码添加到路由并将comment._id更新为comment_id。我认为这就是你把html传递给控制器​​的意思吗?当我这样做时,我得到一个参考错误,说明comment_id没有定义。 – Andy

+0

将这个'console.log(“params.comment_id”,params.comment_id);''改为'console.log(“params.comment_id”,req.params.comment_id);' – Vignesh

+0

将路由和函数添加到:message:'在路径“_id”处,投射到ObjectId的值未定义“未定义”。在云9中工作,如果这有所作为。 – Andy

相关问题