为什么返回此YelpCamp代码“ TypeError:无法读取未定义的属性(Reading' push;);

发布于 2025-02-08 09:02:54 字数 2403 浏览 0 评论 0原文

我只是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;

ErrorMessage:
ErrorMessage

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2025-02-15 09:02:55

您正在尝试推出一个undefined的数组,在此行中出现问题

campground.comments.push(comment);

。它的未定义

You are trying to push in an array that is undefined, problem occurs in this line

campground.comments.push(comment);

You have to check if the campground.comments array exists before pushing, and initialize it in case its undefined

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文