在允许值Meteor中使用函数

时间:2015-11-02 11:25:26

标签: meteor meteor-autoform meteor-collection2

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

现在当我尝试使用像这样的

进行插入时
{{> quickForm collection="Competitions" id="insertTeamForm" type="insert"}}

我没有获得可供选择的团队列表。我在这里做错了吗?

Team Schema

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

1 个答案:

答案 0 :(得分:1)

allowedValues需要一个数组,并且您正在向它传递函数。如果要返回数组,则无关紧要,因为未调用该函数。您可以像这样使用Immediatelly invoked function



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