这是我的campgrounds.js。
我正在网站上工作,我正在该网站上创建露营地。
每个人都可以通过提供他们的名字,链接和描述来发布他们的露营地。
在此之后,他们可以根据需要编辑和更新露营地。
我的问题是当我去露营地/ id /编辑页面并点击提交时说它不能发布特定的露营地/ id
var express = require("express");
var router = express.Router();
var Campground = require("../models/campground"),
Comment = require("../models/comment");
//INDEX - show all campgrounds
router.get("/", function(req, res){
// Get all campgrounds from DB
Campground.find({}, function(err, allCampgrounds){
if(err){
console.log(err);
} else {
res.render("campgrounds/index",{campgrounds:allCampgrounds});
}
});
});
//CREATE - add new campground to DB
router.post("/",isLoggedIn, function(req, res){
// get data from form and add to campgrounds array
var name = req.body.name;
var image = req.body.image;
var desc = req.body.description;
var author = {
id:req.user._id,
username:req.user.username
}
var newCampground = {name: name, image: image, description: desc , author:author}
// Create a new campground and save to DB
Campground.create(newCampground, function(err, newlyCreated){
if(err){
console.log(err);
} else {
//redirect back to campgrounds page
console.log(newlyCreated);
res.redirect("/campgrounds");
}
});
});
//NEW - show form to create new campground
router.get("/new",isLoggedIn, function(req, res){
res.render("campgrounds/new");
});
// SHOW - shows more info about one campground
router.get("/:id", function(req, res){
//find the campground with provided ID
Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground){
if(err){
console.log(err);
} else {
console.log(foundCampground)
//render show template with that campground
res.render("campgrounds/show", {campground: foundCampground});
}
});
});
//EDIT Campground Route
router.get("/:id/edit", function(req,res){
Campground.findById(req.params.id,function(err,foundCampground){
if(err){
res.redirect("/campgrounds");
} else{
res.render("campgrounds/edit",{campground:foundCampground});
}
})
})
//UPDATE campground Route
router.put("/:id",function(req,res){
Campground.findByIdAndUpdate(req.params.id,function(err,updatedCampground){
if(err){
res.redirect("/campgrounds")
} else{
res.redirect("/campgrounds/" + req.params.id );
}
})
})
//middleware
function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
return next();
}
res.redirect("/login");
}
module.exports = router;
//this is my edit.ejs file
<% include ../partials/header %>
<div class="container">
<div class="row">
<h1 style="text-align: center">Edit <%= campground.name %></h1>
<div style="width: 30%; margin: 25px auto;">
<form action="/campgrounds/<%= campground._id %>?_method=PUT" method="POST">
<div class="form-group">
<input class="form-control" type="text" name="campground[name]" value="<%= campground.name %>">
</div>
<div class="form-group">
<input class="form-control" type="text" name="campground[image]" value="<%= campground.image %>">
</div>
<div class="form-group">
<input class="form-control" type="text" name="campground[description]" value="<%= campground.description %>">
</div>
<div class="form-group">
<button class="btn btn-lg btn-primary btn-block">Submit!</button>
</div>
</form>
<a href="/campgrounds">Go Back</a>
</div>
</div>
</div>
<% include ../partials/footer %>