2016-11-15 3 views
1

populate 메서드를 사용하여 2 개의 컬렉션을 조인하려고 할 때 3 가지 모델 아티스트, 앨범 및 노래가 있습니다. 내 mongodb 연결은 server.js에서 이루어집니다.채우기 할 때 몽구스 스키마가 모델에 등록되지 않았습니다

var mongoose = require('mongoose'); 
//var Album = mongoose.model('Album').schema; 
console.log(Album); 
var artistsSchema = mongoose.Schema({ 
    name:{ 
    type:String, 
    required:true 
    }, 
    albums:{ 
    type:[{type:mongoose.Schema.Types.ObjectId,ref :'Album'}] 
    } 

}); 
var Artist = mongoose.model('Artist',artistsSchema); 
module.exports.getArtistsFull = function(callback,limit) 
{ 
    Artist.find().limit(1) 
     .populate({ 
     path:'albums' 
     ,populate :{path:'albums'} 
    }).exec(callback); 
} 

그리고 내 앨범 모델 : 내 아티스트 모델은 내가 Artist.getArtistsFull 함수를 호출 할 때마다

var mongoose = require('mongoose'); 
var albumsSchema = mongoose.Schema({ 
    name:{ 
    type:String, 
    required:true 
    }, 
    songs:{ 
    type:[{type:mongoose.Schema.Types.ObjectId,ref :'Song'}] 
    } 
}); 
var Album = mongoose.model('Albums',albumsSchema); 
module.exports.getAlbumsWithSongs = function(id,callback,limit){ 
    Album.findById(id) 
     .populate({ 
     path : 'songs', 
     populate :{path:'songs'} 
    }).exec(callback); 
} 

내가 "스키마 모델 앨범에 등록되지 않은"오류가 발생, 내가 사용 console.log (Album) 내 앨범 변수에 아티스트 모델 내에서 작성된 함수가 있지만 앨범에 액세스 할 수 없습니다 .Schema.

+0

사용 모델의 정적 기능으로 변경 단순한 오타했다 : //mongoosejs.com/docs/2.7.x/docs/methods-statics.html – styopdev

답변

0

이 문제는 실제로

var Album = mongoose.model('Albums',albumsSchema); 

내보내고 HTTP를 요구하는 대신`mongoose.model`를 사용하여 모델을 요구하는 대신 할당 기능의

var Album = mongoose.model('Album',albumsSchema); 
관련 문제