2017-01-09 113 views
0

我试图用collection2从其他集合中抽取array。我已经能够使用用户下面的示例对象做到这一点:流星交叉收集阵列

\t users: { 
 
\t type: String, 
 
\t label: "Inspector", 
 
\t optional: true, 
 
\t autoform: { 
 
\t  firstOption: 'Choose an Inspector', 
 
\t  options: function() { 
 
\t  return Meteor.users.find({}, { 
 
\t   sort: { 
 
\t   profile: 1, 
 
\t   firstName: 1 
 
\t   } 
 
\t  }).map(function(c) { 
 
\t   return { 
 
\t   label: c.profile.firstName + " " + c.profile.lastName, 
 
\t   value: c._id 
 
\t   }; 
 
\t  }); 
 
\t  } 
 
\t } 
 
\t },

我愿做相同的,但对于对象的数组。这里是源数据的样子:

{ 
 
    "_id": "xDkso4FXHt63K7evG", 
 
    "AboveGroundSections": [{ 
 
    "sectionName": "one" 
 
    }, { 
 
    "sectionName": "two" 
 
    }], 
 
    "AboveGroundItems": [{ 
 
    "itemSection": "one", 
 
    "itemDescription": "dfgsdfg", 
 
    "itemCode": "dsfgsdg" 
 
    }, { 
 
    "itemSection": "two", 
 
    "itemDescription": "sdfgsdfg", 
 
    "itemCode": "sdfgsdgfsd" 
 
    }] 
 
}

这里是我的功能是什么样子:

agSection: { 
 
    type: String, 
 
    optional: true, 
 
    autoform: { 
 
     firstOption: 'Select A Section Type', 
 
     options: function() { 
 
     return TemplateData.find({}, { 
 
      sort: { 
 
      AboveGroundSections: 1, 
 
      sectionName: [0] 
 
      } 
 
     }).map(function(c) { 
 
      return { 
 
      label: c.AboveGroundSections.sectionName, 
 
      value: c.AboveGroundSections.sectionName 
 
      } 
 
     }); 
 
     } 
 
    } 
 
    },

我知道这一点,它只是不为我拉数据。我相信,我只是缺少一些小东西。我正试图拉取AboveGroundSection阵列内的所有对象。

回答

0

.map()被遍历集合文件但不超过阵列每个文档里面。我也不认为你的排序会按照你希望的方式工作,因为内在的嵌套。

尝试:

agSection: { 
    type: String, 
    optional: true, 
    autoform: { 
    firstOption: 'Select A Section Type', 
    options() { 
     let opt = []; 
     TemplateData.find().forEach(c => { 
     c.AboveGroundSections.forEach(s => { opt.push(s.sectionName) }); 
     }); 
     return opt.sort().map(o => { return { label: o, value: o } }); 
    } 
    } 
}, 

此外,如果您AboveGroundSections阵列只有每个元素一个键,然后您可以简化:

"AboveGroundSections": [ 
    { "sectionName": "one" }, 
    { "sectionName": "two" } 
] 

要:

"AboveGroundSections": [ 
    "one", 
    "two" 
]