2017-01-09 115 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" 
]