2011-12-30 6 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 

업데이트

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

작동 코드처럼 MovieSchema를 OwnedTag에서 제거하고 재 작성 당함입니다 https://gist.github.com/1541219

답변

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 데이터베이스의 강점에서 한발 더 멀어지고 있습니다.하지만 아마도 이것이 가능할 것입니다. 위에 두 번째 시도를 추가하십시오. – 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] 
}); 

이동을.