Mongoose对象文字的Subdoc不可访问

使用Mongoose,如何访问数组中的子文档? 在架构中,我已经根据文档使用对象字面值来声明我的子文档 。

我已经检索到父文档后,可以注销doc.children并查看对象数组,但是当我试图访问任何文档时,我得到的是未定义的。 doc.children不作为数组返回,所以如何访问子文档?

架构:

var parentSchema = new Schema({ children: [{ name: 'string' }] }); 

用法:

 console.log(doc.children); //[{name: 'hello'}, {name: 'world'}] doc.children[0]; //undefined doc.children['0']; //undefined 

上面的代码看起来几乎完全像这里的文档: http : //mongoosejs.com/docs/subdocs.html

我在想也许在你的代码中还有别的东西在你这里没有显示出来。 也许尝试使用get: http : //mongoosejs.com/docs/api.html#document_Document-get

 doc.get('children')