在允许值 Meteor 中使用函数
Use function in allowed values Meteor
在我的竞赛模式中,我需要添加团队列表。我已经定义了一个 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(竞赛架构);
现在,当我尝试像这样使用自动表单插入时
{{> 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);
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
}
});
在我的竞赛模式中,我需要添加团队列表。我已经定义了一个 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(竞赛架构);
现在,当我尝试像这样使用自动表单插入时
{{> 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);
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
}
});