Мой почтовый маршрут не работает после отправки?Я хочу обновить свои данные - PullRequest
0 голосов
/ 07 июня 2018

Это мой campgrounds.js.

Я работаю над сайтом и создаю кемпинги на этом сайте.

Каждый человек может разместить свой кемпинг, указав свое имя, ссылку и описание.

После этого он может редактировать и обновлять кемпинг, если он хочет.

Моя проблема заключается в том, что когда я захожу на страницу кемпингов / id / edit и нажимаю "Отправить", она говорит, что не может опубликовать конкретный кемпинг / 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 %>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...