猫鼬返回[Object]而不是实际的嵌入式文档 [英] Mongoose returning [Object] instead of actual embedded document

查看:68
本文介绍了猫鼬返回[Object]而不是实际的嵌入式文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我填充集合中的引用时,引用的集合中的嵌入文档将显示为[Object],而不是实际文档.

When i populate a reference in a collection, the embedded documents in the referenced collection show up as [Object] instead of the actual document.

更多详细信息

我有一个歌曲模式

var songSchema=new Schema({
    songName:String
});

相册模式

 var albumSchema=new Schema({
    title:String,
    favs:Number,
    songs:[songSchema]
})

和引用专辑的播放列表模式.

and a Playlist Schema which references the albums.

  var playlistSchema=new Schema({
    title:String,
    items: { type: Schema.ObjectId, ref: 'Album' }
})

现在,当我运行以下查询时

Now when i run the following query

 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]数组,而不是实际的嵌入式对象.如何显示实际文件?

Notice how the songs array has an array of [Object] instead of the actual embedded objects. How do i get actual documents to show up ?

推荐答案

歌曲"嵌套了两个以上级别,因此默认情况下,输出由"[Object"]表示.尝试这样做:

The "songs" are nested more than 2 levels, so by default the output is represented by "[Object"]. Try doing this:

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);
  })
});

这篇关于猫鼬返回[Object]而不是实际的嵌入式文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆