我正在尝试构建一个简单的网站,用户可以在该网站上发布图片,其他用户也可以对该图片发表评论,但是在引用其他架构时我一直停留在该网站上。
针对camground.js的模式,签入camgrounds集合时注释的数组为空。
var mongoose = require("mongoose");
var commentSchema = mongoose.Schema({
text: String,
author: String
});
module.exports = mongoose.model("Comment", commentSchema);
评论模式。
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://farm4.staticflickr.com/3859/15123592300_6eecab209b.jpg",
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 I wish there was internet",
author: "Homer"
}, function(err, comment){
if(err){
console.log(err);
} else {
campground.comments.push(comment);
campground.save();
console.log("Created new comment");
// console.log(comment);
}
});
}
});
});
});
//add a few comments
}
module.exports = seedDB;
我已经在seeds.js中对注释进行了硬编码,并在此处使用了填充,但是camcollection集合没有注释的对象ID,请帮忙。
url
答案 0 :(得分:0)
代替
QString
做
import sip
sip.setapi('QString', 1)
编辑:
编写一个快速脚本来测试您的方案。请参见下面的脚本。它可以很好地将所有实体与注释一起保存到数据库。运行脚本后从mongo附加了结果。检查我是否想念东西
campground.comments.push(comment);
结果:
campground.comments.push(comment._id);