【问题标题】:Use function in allowed values Meteor在允许的值中使用函数 Meteor
【发布时间】: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


【解决方案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
}
});

【讨论】:

  • 我一到家就试试这个。谢谢老兄。
  • 它没有被调用。
  • 我已经更新了我的问题并添加了几个 cmets 供您查看。
  • 我想通了,那里无法访问 Teams。我想我必须订阅它,但不知道如何订阅收藏中的出版物。
  • 如果我在我的项目中再次添加自动发布,它会开始工作,但一旦我删除它就会停止。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-22
  • 2022-01-03
  • 2016-09-01
  • 2014-11-27
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
相关资源
最近更新 更多