2017-01-09 3 views
0

Ich versuche, eine array aus einer anderen Sammlung mit collection2 zu ziehen. Ich konnte dies mit Objekten tun, das folgende Beispiel für Benutzer mit:Meteor Kreuz Sammlung Arrays

\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 },

Ich möchte das gleiche tun, sondern für eine Reihe von Objekten. Hier ist, was die Quelldaten wie folgt aussieht:

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

Hier ist, was meine Funktion wie folgt aussieht:

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

ich das weiß, dann ist es einfach nicht ziehe die Daten für mich. Ich bin mir sicher, ich vermisse nur etwas Kleines. Ich versuche, alle Objekte innerhalb des AboveGroundSection Arrays zu ziehen.

Antwort

0

Ihre .map() iteriert über den Satz von Dokumente aber nicht über die Arrays in jedem Dokument. Ich glaube auch nicht, dass Ihre Sortierung so funktioniert, wie Sie es von der inneren Verschachtelung erwarten.

Versuchen:

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

Auch wenn Ihr AboveGroundSections Array nur einen einzigen Schlüssel pro Element hat, dann können Sie vereinfachen:

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

An:

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