nodejs推送未定义

时间:2018-04-10 00:21:14

标签: javascript node.js push

.infoCard {
    display: none;
    position: absolute;
}

.glyphicon-info-sign:hover + .infoCard {
    display: block;
}

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

2 个答案:

答案 0 :(得分:0)

不知道露营地是什么,很难说清楚。

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

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

campground.save()存在之前是否需要调用campground.comment?在该回调函数console.log

中尝试Comment.create'你的露营地对象

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

 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");
    }
  });

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

答案 1 :(得分:0)

尝试一下

 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");
                             }
                        });

如果不行,请向我展示露营地模型