集合未保存在 mongodb 中(猫鼬填充文档中的故事集合)

Collection isn't being saved in mongodb (Story collection from the mongoose populate documentation)

下面的代码给我这个错误:

  console.log("The creator is %s", story._creator.name)
                                        ^

  TypeError: Cannot read property '_creator' of null

var express = require("express");
var app = express();
var mongoose = require("mongoose");
var Schema = mongoose.Schema;

mongoose.connect("mongodb://localhost/pop2");


var personSchema = Schema({
    _id : Number,
    name : String,
    age : Number,
    stories  : [{type : Schema.Types.ObjectId, ref : "Story"}]
})


var storySchema = Schema({
    _creator : {type : Number, ref : "Person"},
    title : String,
    fans : [{type : Number, ref : "Person"}]
});

var Story = mongoose.model("Story", storySchema);
var Person = mongoose.model("Person", personSchema);


var aaron = new Person({_id : 0, name :"Aaron", age : 100});

aaron.save(function(err){
    if(err) return handleError(err)

    var story1 = new Story({
        title : "Once upon a timex.",
        _creator : aaron._id
    });

    story1.save(function(err){
        if(err) return handleError(err);
        console.log("saved") //doe snot output saved
    })
})

Story.findOne({title : "Once upon a timex."})
        .populate("_creator")
        .exec(function(err, story){
            console.log(story)// I get an output of null in console could be from this
            if(err) return handleError(err);
            console.log("The creator is %s", story._creator.name)}) // error from above



app.listen(3000, function(){
    console.log("listening on 3000")
})

我只是想遵循 populate docs。所以我想要一个与该演示一起使用的解决方案。我找不到我缺少的东西。

当我做 show collections 时,我只看到 peoplesystem.indexes 集,而不是故事集

EDIT :: 我将以下内容放入 else 部分的 aaron.save() 中,但出现错误

{ [MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: pop2.people.$_id_ dup key: { : 0 }]name: 'MongoError', message: 'insertDocument :: caused by :: 11000 E11000 duplicate key error index

    Story.findOne({title : "Once upon a timex."})
            .populate("_creator")
            .exec(function(err, story){
                // console.log(story)
                if(err) 
                    console.log(err);
                else{
                    console.log("mine : ",story)
                    console.log("The creator is %s", story._creator.name)
                }

            })

我想,既然它说 "duplicate" 我应该删除数据库并重新启动服务器,我就得到了文档。所以那很好。但是我试图重新启动服务器以再次保存它,但我再次遇到重复错误。现在我需要再次删除这些集合。为什么我会收到此重复错误。如果我只是重新保存普通文档,我不会收到此错误。

我认为这可以解决问题。

var express = require("express");
var app = express();
var mongoose = require("mongoose");
var Schema = mongoose.Schema;

mongoose.connect("mongodb://localhost/pop2");


var personSchema = Schema({
    _id : Number,
    name : String,
    age : Number,
    stories  : [{type : Schema.Types.ObjectId, ref : "Story"}]
})


var storySchema = Schema({
    _creator : {type : Number, ref : "Person"},
    title : String,
    fans : [{type : Number, ref : "Person"}]
});

var Story = mongoose.model("Story", storySchema);
var Person = mongoose.model("Person", personSchema);


var aaron = new Person({_id : 0, name :"Aaron", age : 100});

aaron.save(function(err){
    if(err) return handleError(err)    
});

var story1 = new Story({
    title : "Once upon a timex.",
    _creator : aaron._id
});

story1.save(function(err){
    if(err) return handleError(err);
    console.log("saved")
});

Story.findOne({title : "Once upon a timex."})
        .populate("_creator")
        .exec(function(err, story){
            console.log(story);
            if(err) return handleError(err);
            console.log("The creator is %s", story._creator.name)})



app.listen(3000, function(){
    console.log("listening on 3000")
});

只需检查一下,看看它是否有效。

试试我的代码,它有效。

var mongoose = require('mongoose')
  , Schema = mongoose.Schema
mongoose.connect('mongodb://localhost/test'); 
var personSchema = new Schema({
  _id     : Number,
  name    : String,
  age     : Number,
  stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

var storySchema = new Schema({
  _creator : { type: Number, ref: 'Person' },
  title    : String,
  fans     : [{ type: Number, ref: 'Person' }]
});

var Story  = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);

//saving refs
var aaron = new Person({ _id: 0, name: 'Aaron', age: 100 });

aaron.save(function (err) {
  if (err) return handleError(err);

  var story1 = new Story({
    title: "Once upon a timex.",
    _creator: aaron._id    // assign the _id from the person
  });

  story1.save(function (err) {
    if (err) return handleError(err);
    // thats it!
  }).then(function(){
    Story.findOne({ title: 'Once upon a timex.' })
        .populate('_creator')
        .exec(function (err, story) {
        if (err) return handleError(err);
        console.log('The creator is %s', story._creator.name);
        // prints "The creator is Aaron"
        });
    });
});

基本上保存是异步的。当你打电话时 Story.findOne 故事还没有保存下来