如何从父实例访问mongoose中的子文档实例?

时间:2016-02-20 10:38:53

标签: node.js mongodb mongoose

   var mongoose = require('mongoose');
   var Schema = mongoose.Schema;

   var ChildSchema = new Schema({
       name: String
   });

   ChildSchema.methods.getName = function () {
       return this.name;
   }

   var ParentSchema = new Schema({
       children: {
            type: [ChildSchema]
            default: []
       }
   });

   ParentSchema.methods.getChildName = function () {
       // How to facilitate ability to access instance of ChildSchema to call child.getName
   }
   var Parent = mongoose.model('Parent', ParentSchema);

在上面的代码中,我希望在getName中访问ChildSchema的{​​{1}}方法。如何在猫鼬中完成?

提前多多感谢。

1 个答案:

答案 0 :(得分:0)

请尝试使用"<tr><td>"+myDataFromDatabase+"</td>....</tr>"作为参数,如下所示。

subdocument index