我是后期开发的新手..我在这里面临一个问题:
app.js
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var Campground = require("./models/campground");
var Comment = require("./models/comment");
var seedDB = require("./seeds");
mongoose.Promise = require('bluebird');
mongoose.connect("mongodb://localhost/yelp_camp", {
useMongoClient: true
});
app.use(bodyParser.urlencoded({
extended: false
}));
app.set("view engine", "ejs");
seedDB();
app.get("/", function(req, res) {
res.render("landing");
});
app.get("/campgrounds", function(req, res) {
Campground.find({}, function(err, allCampgrounds) {
if (err) {
console.log("ERROR!!");
} else {
res.render("campgrounds/campgrounds", {
campgrounds: allCampgrounds
});
}
});
});
app.post("/campgrounds", function(req, res) {
// get info from form;
var name = req.body.name;
var image = req.body.image;
var description = req.body.description;
var newCampground = {
name: name,
image: image,
description: description
};
Campground.create(newCampground)
res.redirect("/campgrounds");
});
app.get("/campgrounds/new", function(req, res) {
res.render("campgrounds/new");
});
app.get("/campgrounds/:id", function(req, res) {
Campground.findById(req.params.id).populate("comments").exec(function(err, foundCampground) {
if (err) {
res.redirect("/campgrounds");
} else {
res.render("campgrounds/info", {
campground: foundCampground
});
}
});
});
app.get("/campgrounds/:id/comments/new", function(req, res) {
Campground.findById(req.params.id, function(err, campground) {
if (err) {
console.log("err");
} else {
res.render("comments/new", {
campground: campground
});
}
});
});
app.post("/campground/:id/comments", function(req, res) {
Campground.findById(req.params.id, function(err, campground) {
if (err) {
console.log("error!!!");
res.redirect("/campgrounds");
} else {
Comment.create(req.body.comment, function(err, comment) {
if (err) {
console.log("error!!!!!")
} else {
campground.comments.push(comment);
campground.save();
res.redirect("/campgrounds/" + campground._id);
}
});
}
});
});
app.listen("3000", function() {
console.log("THE YELPCAMP SERVER HAS STARTED!!!");
});
seeds.js
var mongoose = require("mongoose");
var Campground = require("./models/campground");
var Comment = require("./models/comment");
var data = [
{
name: "Cloud",
image:"http://www.itworldcanada.com/wp-content/uploads/2017/05/cp1_0111.jpg",
description:"very beautiful!!"
},
{
name: "Dessert Rose",
image:"http://glennbates.com/desertrose/images/Desert-Rose-Plant-Care.jpg",
description:"very beautiful!!"
},
{
name: "SKY!",
image:"http://images.boomsbeat.com/data/images/full/1754/1-jpg.jpg",
description:"very beautiful!!"
}
]
function seedDB(){
Campground.remove({}, function(err){
if(err){
console.log(err);
}
console.log("Campground Deleted!!");
data.forEach(function(seed){
Campground.create(seed, function(err, campground){
if(err){console.log("error!!");}
else {
console.log("Added A Campground");
//ADD COMMENT!!
Comment.create({
text: "This Place is great , but There was no internet",
author: "Obada"
}, function(err, comment){
if(err){console.log(err)}
else {
campground.comments.push(comment);
campground.save();
console.log("Created a comment");
}
});
}
});
});
});
}
module.exports = seedDB;
/models/comment.js
var mongoose = require("mongoose");
var commentSchema =new mongoose.Schema({
text: String,
author: String
});
module.exports = mongoose.model("Comment", commentSchema);
/models/campground.js
var mongoose = require("mongoose");
var campgroundsSchema =new mongoose.Schema({
name: String,
image: String,
description: String,
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
]
});
module.exports = mongoose.model("Campground", campgroundsSchema);
当我在端口3000上运行时,它给了我"不能POST /露营地/(id)" P.S:我正在使用codeanywhere。
答案 0 :(得分:0)
您没有定义必要的路线。添加此
app.post("/campgrounds/:id", function(req, res) {
Campground.create(req.body, function (err, small) {
if (err) console.log(err);
res.redirect('/campgrounds/' + small._id);
})
}):
答案 1 :(得分:0)
在注释dir,new.ejs文件中,您可能错过了/评论-----
<form action="/campgrounds/<%= campground._id %>/comments" method="post">