【发布时间】:2019-01-23 03:04:26
【问题描述】:
我无法将 cmets 推送到数据数组。 cmets 已创建(已添加到评论集合中)。如何将它们推送到数据数组?这是我的代码的一些屏幕截图:Comments Schema 和 ,The comments array is empty, Camp Schema
var mongoose = require("mongoose");
var camp = require("./models/camp");
var Comment = require("./models/comment");
var data = [
{
name: "Cloud's Rest",
image: // a link goes here
},
{
name: "Desert Mesa",
image: //
},
{
name: "Canyon Floor",
image: //
}
];
function seedDB(){
//Remove all campgrounds that existed in the database & create new campgrounds with data array.
camp.remove({}, function(err){
if(err){
console.log(err);
}
console.log("removed campgrounds!");
//add a few campgrounds
data.forEach(function(seed){
camp.create(seed, function(err, camp){
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 {
camp.comments.push(comment);
camp.save();
console.log("Created new comment");
}
});
}
});
});
});
//add a few comments
}
module.exports = seedDB;
【问题讨论】:
-
欢迎来到 SO。如果您打算在此处发布代码,则可以考虑先删除语法错误。我们都意识到节点组件不会加载等,但错误与
}有关。现在,您似乎正在为您的变量传递campground,而不是代码中那个位置的camp。 -
是的,所以 camp.cmets 是未定义的。你没有提供任何表明它应该被定义的东西......所以......按预期工作?
-
嗨@KevinB,不,我不这么认为。请检查我上面的架构。
-
@AnasAl-Sharif 不,错误不是谎言。它是未定义的。
-
@KevinB 我知道错误不会说谎,这就是我首先问这个问题的原因。谢谢。
标签: javascript node.js mongodb