【发布时间】:2015-11-02 11:25:26
【问题描述】:
在我的竞赛模式中,我需要添加团队列表。我已经定义了一个 TeamSchema,并且在集合中添加了一些团队。现在我想添加一个比赛并将团队列表添加到该比赛中。
这就是我的比赛架构的样子
Competitions = new Mongo.Collection("competitions");
var CompetitionsSchema = new SimpleSchema({
year: {
type: String
},
division: {
type : String,
allowedValues: ['Elite', '1st','2nd','3rd','4th','Intro']
},
teams:{
type : [TeamSchema],
allowedValues: (function () {
console.log(1); // this is logged
return Teams.find().fetch().map(function (doc) {
console.log(doc.name); // this is not even logged
return doc.name;
});
}()) //here we wrap the function as expression and invoke it
}
}); Competitions.attachSchema(CompetitionsSchema);
现在,当我尝试使用这样的 autoform 插入时
{{> quickForm collection="Competitions" id="insertTeamForm" type="insert"}}
我没有可供选择的团队列表。我在这里做错了吗?
团队架构
Teams = new Mongo.Collection("teams");
TeamSchema = new SimpleSchema({
name: {
type: String
},
matches: {
type: Number,
defaultValue: 0
},
matchesWon: {
type: Number,
defaultValue: 0
},
matchesLost: {
type: Number,
defaultValue: 0
},
matchesTied: {
type: Number,
defaultValue: 0
},
points: {
type: Number,
decimal: true,
defaultValue: 0
},
netRunRate: {
type: Number,
decimal: true,
defaultValue: 0,
min: -90,
max: 90
},
pointsDeducted: {
type: Number,
optional: true
},
isOurTeam: {
type: Boolean,
defaultValue: false
}
});
Teams.attachSchema(TeamSchema);
【问题讨论】:
-
find()返回光标,map 需要数组。你应该使用find().fetch() -
使用 fetch() 仍然没有运气。
-
另外,您的地图不会改变任何东西(您在对象数组上使用它并返回对象数组),您可能应该返回一些字符串,例如
doc.teamName或 sth -
我可以获得示例代码吗?
-
return Teams.find().fetch().map(function (doc) { return doc.teamName; });
标签: meteor meteor-autoform meteor-collection2