我正在尝试使用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);
}
我的专辑模型:
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);
}
每当我调用Artist.getArtistsFull函数时,我都会收到错误" Schema还没有注册模型Album",我在艺术家模型中的Album变量中使用了console.log(Album),我得到了在那里写的功能,但我无法访问albumsSchema。
答案 0 :(得分:2)
问题实际上是一个简单的错字
var Album = mongoose.model('Albums',albumsSchema);
更改为
var Album = mongoose.model('Album',albumsSchema);