我是Node,Mongoose和与Backend相关的一切的新手。我最近遇到了一个问题。我无法弄清楚,因为开发课程的练习文件中的相同代码似乎是相同的。不过问题仍然没有解决。我想与教程一起编码,就是要为Mongoose数据库中显示的每个露营地部分定义一个Mongoose模型,以供注释。我有models文件夹,可以在其中定义露营地和注释的模型,还有一个seeds.js文件,可以在其中将数据动态添加到露营地(以便使其随即显示注释),然后添加注释。这些文件的实际代码如下所示: app.js->
# A tibble: 5 x 3
# Groups: MMR_NBR [4]
Timestamp MMR_NBR Action
<dttm> <fct> <fct>
1 2018-08-14 11:22:18 W00364 SAP Load
2 2018-08-14 11:30:03 K00364 SAP Load
3 2018-08-14 11:32:26 K00364 SAP Load
4 2018-08-22 16:34:53 K00646 SAP Load
5 2018-08-23 11:47:15 B00218 SAP Load
seeds.js ---->
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require('mongoose');
var Campground=require("./models/campground");
var seedDB = require("./seeds");
var PORT = process.env.IP || 3200;
seedDB();
mongoose.connect('mongodb://localhost/yelp_camp', { useNewUrlParser: true },
(err, res) => {
if (err) throw err;
console.log('Database online');
});
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
//schema
app.get("/", function (req, res) {
res.render("landing");
});
app.get("/campgrounds/tornike", function (req, res) {
//get all campgrounds
res.send(Campground.name);
});
app.get("/campgrounds", function (req, res) {
//get all campgrounds
Campground.find({}, function (err, camp) {
if (err) {
console.log("ERROR");
console.log(err);
} else {
res.render("index", { campgrounds: camp });
}
});
});
app.post("/campgrounds", function (req, res) {
var name = req.body.name;
var image = req.body.image;
var desc = req.body.description;
var newCampground = { name: name, image: image, description:desc };
//create new camp and save to database
Campground.create(newCampground, function (err, newlyCreated) {
if (err) {
console.log(err);
} else {
res.redirect("/campgrounds");
}
});
});
app.get("/campgrounds/new", function (req, res) {
res.render("new.ejs");
});
//shows more info about camp
app.get("/campgrounds/:id", function (req, res) {
//find camp with provided id
Campground.findById(req.params.id, function (err, foundCampground) {
if (err) {
console.log(err);
} else {
//render show template
res.render("show", { campground: foundCampground });
}
});
});
app.listen(PORT, process.env.IP, function () {
console.log("camp");
});
comment.js --->
var mongoose = require("mongoose");
var Campground = require("./models/campground");
var Comment = require("./models/comment");
var data = [
{
name: "something",
image: "image URL",
description: "blah blah bla1"
},
{
name: "something",
image: "image URL",
description: "blah blah bla2"
},
{
name: "something",
image: "image URL",
description: "blah blah bla3"
}
];
function seedDB() {
Campground.deleteMany({}, function(err) {
console.log("removed camps");
data.forEach(function(seed) {
Campground.create(seed, function(err, data) {
if (err) {
console.log(err);
} else {
console.log("added campground");
Comment.create(
{
text: "dubdabdubadah",
author: "Homer"
},
function(err, comment) {
if (err) {
console.log(err);
} else {
campground.comments.push(comment);
campground.save();
console.log("Created new comment");
}
}
);
}
});
});
});
}
module.exports = seedDB;
campground.js ---->
var mongoose = require("mongoose");
var commentSchema= new mongoose.Schema({
text:String,
author:String
});
module.exports=mongoose.model("Comment", commentSchema);
此行上的seed.js文件中发生错误campground.comments.push(comment); 看起来像这样:
var mongoose = require("mongoose");
var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
description: String,
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
]
});
module.exports = mongoose.model("Campground", campgroundSchema);
与将代码与练习文件的代码进行比较时相比,我实际上无法绕开它-一样。我唯一能想到的是猫鼬express的版本,或者其他可能不相关或不赞成使用某些方法的方法,但老实说,不知道出了什么问题。我已经尝试解决了好几天。有什么想法的人吗?
答案 0 :(得分:1)
错误消息告诉您,您尚未定义名为campground
的变量,而您没有定义它是正确的。
要解决此问题,请在data
回调中将campground
更改为Campground.create
:
Campground.create(seed, function(err, campground) {