【问题标题】:nodejs push of undefinednodejs推送未定义
【发布时间】:2018-04-10 00:21:13
【问题描述】:
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 I wish there was internet",
                            author: "Homer"
                        }, function(err, comment){
                            if(err){
                                console.log(err);
                            } else {
                                **campground.comment.push(comment);**
                                campground.save();
                                console.log("Created new comment");
                            }
                        });
                }
            });
        });
    }); 
    //add a few comments
}

module.exports = seedDB;

TypeError:无法读取未定义的属性“推送” 有人可以帮我吗? 我不知道这个功能有什么问题? 为什么他看不懂属性推送

【问题讨论】:

  • 什么是Campground
  • 建议对function(err,comment){...}使用箭头函数。例如(err, comment) -> { } 这是因为它没有自己的范围可以隐藏campground
  • @SamuelToh 范围在这里很好,没必要。
  • 那么我能看到的唯一问题是campground.comment 未定义,因此当您执行campground.undefined.push 时会出错。也许尝试转储campground.push?确保它存在

标签: javascript node.js push


【解决方案1】:

如果不知道 Campground 是什么,就很难分辨。

请注意,当您看到错误 TypeError: Cannot read property 'someProperty' of undefined 时,它不是未定义的属性 someProperty,而是属性所在的对象。在这种特殊情况下:campground.comment

我想到的一件事是,Comment.create 函数似乎有一个回调作为第二个参数。在该回调中,是否创建了评论?如果在回调运行时还没有创建评论,那么可能 campground.comment 还不存在。

campground.save() 是否需要在campground.comment 存在之前被调用?在回调函数Comment.create 中尝试console.log'ing 您的露营地对象@

也许你需要手动设置评论。像这样的:

 Comment.create({
    text: "This place is great, but I wish there was internet",
    author: "Homer"
  }, function (err, comment) {
    if (err) {
      console.log(err);
    } else {
      if(campground.comment) {
        campground.comment.push(comment);
      } else {
        campground.comment = [comment];
      }
      campground.save();
      console.log("Created new comment");
    }
  });

如果您的目标是在已有 cmets 的情况下附加 cmets,或者如果没有,则使用您的第一条评论创建一个全新的数组。

【讨论】:

    【解决方案2】:

    试试这个

     Comment.create(
                             {
                                 text: "This place is great, but I wish there was internet",
                                 author: "Homer"
                             }, function(err, commenttext){
                                 if(err){
                                     console.log(err);
                                 } else {
                                     campground.comment.push(commenttext);
                                     campground.save();
                                     console.log("Created new comment");
                                 }
                            });
    

    如果不行……给我看看露营地模型

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多