为什么返回此YelpCamp代码“ TypeError:无法读取未定义的属性(Reading' push;);
我只是WebDev的一名学生,我已经尝试对其进行纠正,但我很感激如果这里被征用的成员可以帮助我纠正此错误,则该代码假设将评论推向可用的每个营地阵列。 评论注释。
var mongoose = require("mongoose");
var Campground = require("./models/campground");
var Comment = require("./models/comment");
var data = [
{
name: "Cloud's Rest",
image: "https://farm4.staticflickr.com/3795/10131087094_c1c0a1c859.jpg",
description: "blah blah blah"
},
{
name: "Desert Mesa",
image: "https://images.unsplash.com/photo-1557214997-7eae7e0e7aaa?ixlib=rb-1.2.1& ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8ZGVzZXJ0JTIwbWVzYXxlbnwwfHwwfHw%3D&auto=format&fit=crop&w=400&q=60",
description: "blah blah blah"
},
{
name: "Canyon Floor",
image: "https://farm1.staticflickr.com/189/493046463_841a18169e.jpg",
description: "blah blah blah"
}
]
function seedDB(){
//Remove all campgrounds
Campground.remove({}, function(err){
if(err){
console.log(err);
}
console.log("removed campgrounds!");
//add a few 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 facilities are not good",
author: "John Paul"
}, function(err, comment){
if(err){
console.log(err);
} else {
campground.comments.push(comment);
campground.save();
console.log("Created new comment");
}
});
}
});
});
});
}
module.exports = seedDB;
@haris:请检查下面的campground.js,我想评论数组定义为
var mongoose = require(“ mongoose”);
//架构设置 var CampgroundSchema = new Mongoose.schema({{ 名称:字符串, 图像:字符串, 描述:字符串, 评论: [ { 类型:mongoose.schema.types.objectid, 参考:“评论” } 这是给出的 });
Module.exports = Mongoose.model(“营地”,CampgroundSchema);
I am just a student in webdev,I already try to correct it but fail, I appreciate if exprienced members here can assist me in correcting this error, the code suppose to push the comment to every campground array available. The app run if I comment out comment.create part(up to console.log("created new comment"). Kindly refer to image for the error message. Thank you
var mongoose = require("mongoose");
var Campground = require("./models/campground");
var Comment = require("./models/comment");
var data = [
{
name: "Cloud's Rest",
image: "https://farm4.staticflickr.com/3795/10131087094_c1c0a1c859.jpg",
description: "blah blah blah"
},
{
name: "Desert Mesa",
image: "https://images.unsplash.com/photo-1557214997-7eae7e0e7aaa?ixlib=rb-1.2.1& ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8ZGVzZXJ0JTIwbWVzYXxlbnwwfHwwfHw%3D&auto=format&fit=crop&w=400&q=60",
description: "blah blah blah"
},
{
name: "Canyon Floor",
image: "https://farm1.staticflickr.com/189/493046463_841a18169e.jpg",
description: "blah blah blah"
}
]
function seedDB(){
//Remove all campgrounds
Campground.remove({}, function(err){
if(err){
console.log(err);
}
console.log("removed campgrounds!");
//add a few 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 facilities are not good",
author: "John Paul"
}, function(err, comment){
if(err){
console.log(err);
} else {
campground.comments.push(comment);
campground.save();
console.log("Created new comment");
}
});
}
});
});
});
}
module.exports = seedDB;
@Haris: kindly check campground.js below, I guess the comments array is defined
var mongoose = require("mongoose");
//Schema Setup
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试推出一个
undefined
的数组,在此行中出现问题。它的
未定义
You are trying to push in an array that is
undefined
, problem occurs in this lineYou have to check if the
campground.comments
array exists before pushing, and initialize it in case itsundefined