1

在我的竞赛模式中,我需要添加团队列表。我已经定义了一个 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);
4

1 回答 1

1

allowedValues 需要一个数组,并且您正在向它传递一个函数。是否返回数组无关紧要,因为未调用该函数。您可以像这样使用立即调用的函数

var CompetitionsSchema = new SimpleSchema({
year: {
    type: String
},
division: {
  type : String,
  allowedValues: ['Elite', '1st','2nd','3rd','4th','Intro']
},
teams:{
    type : [TeamSchema],
    allowedValues: (function () {
        return Teams.find().fetch().map(function (doc) { return doc.name; });
    }()) //here we wrap the function as expression and invoke it
}
});

于 2015-11-02T15:14:05.170 回答