1
の「プッシュ」プロパティを読み取ることができません。私はノードを実行したとき、私はnodejsを学ぶので、JavaScriptと私はエラーメッセージをしましたよapp.js.私はすでに、検索が、私はミスをしたところ、まだ見つけていないよ:/エラーメッセージは、未定義
TypeError : Cannot read property 'push' of undefined at C:...\seeds.js:46:52
ここseeds.jsのコード:ここで
const mongoose = require("mongoose");
var Campground = require("./models/campground");
var Comment = require("./models/comment");
var data = [
{
name: "Cloud",
image: "http://rockwoodparkcampground.com/wp-content/uploads/2015/10/campground_033.jpg",
description: "bla bla bla"
},
{
name: "Desert",
image: "http://rockwoodparkcampground.com/wp-content/uploads/2015/10/campground_033.jpg",
description: "bla bla bla"
}
]
function seedDB() {
//Remove all campgrounds
Campground.remove({}, (err) => {
if (err) {
console.log(err);
}
console.log("bla");
// add campgrounds
data.forEach(function (seed) {
Campground.create(seed, function (err, campground) {
if (err) {
console.log(err);
} else {
console.log("added a campground");
// create a comment
Comment.create(
{
text: "This place is great, but I wish there was internet",
author: "Hermione Granger"
}, function (err, comment) {
if (err) {
console.log(err)
} else {
campground.comments.push(comment);
campground.save();
console.log("Created a new comment");
}
});
}
});
});
});
}
module.exports = seedDB;
がcomments.js:
const mongoose = require("mongoose");
var commentSchema = mongoose.Schema({
text: String,
author: String
});
module.exports = mongoose.model("Comment", commentSchema);
をここcampground.js
const mongoose = require('mongoose');
var campgroundSchema = new mongoose.Schema({
name: String,
image: String,
description: String
});
module.exports = mongoose.model("Campground", campgroundSchema);
フィン味方app.js
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var Campground = require("./models/campground");
var seedDB = require("./seeds")
seedDB();
mongoose.connect('mongodb://localhost/beer_n_camp');
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + '/views'));
'campground.comments'は未定義/ nullであり、それは配列のようにあなたは()'その上にプッシュ 'しようとしています。 – Marc
提案:あなたは、おそらく私が見つけた@SeanKnowキャンプ場のスキーマ –
に多くのコメントに明示的にキャンプ場を関連付ける方法を追加する必要があります! コメント:[ { タイプ:mongoose.Schema.Types.ObjectId、 参照: "コメント" } ];ご提案をお寄せいただきありがとうございます)私はちょうどcampgroundSchemaへのコメントの道を持っていました –