2011-12-30 5 views
5

テストコード'populating multiple children of a sub-array at a time'と非常によく似ていますが、なぜ動作していないのかわからないテストコードです。モンゴースが居住しています

var mongoose = require('mongoose'), 
    Schema = mongoose.Schema, 
    ObjectId = Schema.ObjectId; 

mongoose.connect('mongodb://localhost/testy'); 

var UserSchema = new Schema({ 
    name: String 
}); 

var MovieSchema = new Schema({ 
    title: String, 
    tags: [OwnedTagSchema] 
}); 

var TagSchema = new Schema({ 
    name: String 
}); 

var OwnedTagSchema = new Schema({ 
    _name: {type: Schema.ObjectId, ref: 'Tag'}, 
    _owner: {type: Schema.ObjectId, ref: 'User'} 
}); 

var Tag = mongoose.model('Tag', TagSchema), 
    User = mongoose.model('User', UserSchema), 
    Movie = mongoose.model('Movie', MovieSchema); 
    OwnedTag = mongoose.model('OwnedTag', OwnedTagSchema); 

User.create({name: 'Johnny'}, function(err, johnny) { 
    Tag.create({name: 'drama'}, function(err, drama) { 
     Movie.create({'title': 'Dracula', tags:[{_name: drama._id, _owner: johnny._id}]}, function(movie) { 

      // runs fine without 'populate' 
      Movie.find({}).populate('tags._owner').run(function(err, movies) { 
       console.log(movies); 
      }); 
     }); 
    }) 
}); 

プロデュースエラーが

node.js:201 
     throw e; // process.nextTick error, or 'error' event on first tick 
      ^
TypeError: Cannot call method 'path' of undefined 
    at /Users/tema/nok/node_modules/mongoose/lib/model.js:234:44 

で更新

OwnedTagから取り除き、作業コードhttps://gist.github.com/1541219

この

var MovieSchema = new Schema({ 
    title: String, 
    tags: [new Schema({ 
     _name: {type: Schema.ObjectId, ref: 'Tag'}, 
     _owner: {type: Schema.ObjectId, ref: 'User'} 
    })] 
}); 

ようMovieSchemaを書き直しガット

答えて

1

あなたのコードもうまくいくと思います。 OwnedTagMovieSchemaに置くとうまくいきますか?

var MovieSchema = new Schema({ 
    title: String, 
    tags: [{ 
      _name: {type: Schema.ObjectId, ref: 'Tag'}, 
      _owner: {type: Schema.ObjectId, ref: 'User'} 
     }] 
}); 

編集:

var MovieSchema = new Schema({ 
    title: String, 
    tags: [{ type: Schema.ObjectId, ref: 'OwnedTag' }] 
}); 
+0

いいえ、同じ結果:-( – spacevillain

+1

ああ、実際に元のモデルに戻ってください。あなたは '.populate'を使っていますが、refではなく' tags'のサブ文書を使っています。それがうまくいくと期待しています - 私たちはNoSQLデータベースの強みからさらに遠くまで進んでいますが、おそらくこれはうまくいくでしょう - 上記2回目の試みを見てください – danmactough

+0

これは全てを破ります:-(私はhttps://github.com/LearnBoost/mongoose/blob/master/test/model.ref.test.js#L1109の違いを教えてください。 – spacevillain

2

あなたのあなたがそれを使用するか、基本的にこのやって終わるだろう前にOwnedTagSchema変数を定義する必要があります。MovieSchema定義上記

var MovieSchema = new Schema({ 
    title: String, 
    tags: [undefined] 
}); 

移動し、それを。

関連する問題