当我填充集合中的引用时,引用的集合中的嵌入文档将显示为[Object],而不是实际文档。
更多细节
我有一个歌曲模式
var songSchema=new Schema({
songName:String
});
专辑模式
var albumSchema=new Schema({
title:String,
favs:Number,
songs:[songSchema]
})
以及引用专辑的播放列表模式。
var playlistSchema=new Schema({
title:String,
items: { type: Schema.ObjectId, ref: 'Album' }
})
现在,当我运行以下查询
Playlist
.find()
.populate('items')
.exec(function (err, playlists) {
if (err) return handleError(err);
console.log("Result:"+playlists);
})
我得到以下结果
Result:{ _id: 53d6b605842416b83b5fe472,
title: 'Sad',
items:
{ _id: 53d6b605842416b83b5fe471,
title: 'Awaz',
favs: 500,
__v: 0,
songs: [ [Object], [Object] ] },
__v: 0 }
请注意,Songs数组如何具有[Object]数组,而不是实际的嵌入式对象。我如何显示实际文件?
“歌曲”嵌套了两个以上级别,因此默认情况下,输出由“ [Object”]表示。尝试这样做:
playlists[0].songs.forEach(function (song) {
console.log(song);
});
如果您使用快递。
app.get('/playlists', function (req, res, next) {
mongoose.model('Playlist').find().populate('items').exec(function (err, docs) {
if (err) return next(err);
res.json(docs);
})
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句