尝试在删除(或更新)内部使用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;
,你能不能给我请求URL你是如何访问的网址从服务器@ Andy – Vignesh
app.listen(process.env.PORT,process.env.IP,function(){ console.log(“App is running”); })< - 这是你在找什么? @Vignesh – Andy