【发布时间】: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