Meteor AutoForm:如何使用子文档数组更新架构值?

Meteor AutoForm: How to update schema values with array of sub-documents?

我是 Meteor AutoForm 的新手。我想用国家文件更新玩家文件。下面是我的代码。如果我在 AutoForm 中添加 {{> afQuickField name="country"}} 它不起作用。 {{> afQuickField name="name"}} 单独工作正常。我如何在玩家所在国家/地区字段中添加整个国家/地区文档 (_id,slug,name)

JS:

CountrySchema = new SimpleSchema({
    _id: {
        type: String,
        index: true
    },
    slug: {
        type: String,
        max: 100,
        index: true
    },
    name: {
        type: String,
        max: 100
    }
});

Player.attachSchema(new SimpleSchema({
    name: {
        type: String,
        label: "Player name",
        index: true
    },
    country: {
        type: [CountrySchema],
        label: "country",
        max: 5,
        index: true,
        autoform: {
            options: function() {
                return Country.find().fetch().map(function(object) {
                    return {
                        label: object.name,
                        value: object._id
                    };
                });
            }
        }
    }
})); 

HTML:

{{#autoForm id="editplayer" }}
   {{> afQuickField name="name"}}
   {{> afQuickField name="country"}}
{{/autoForm}}

我添加了 SimpleSchema.debug = true; 控制台日志显示 SimpleSchema invalid keys for "editplayer" context

如果您想将 country 呈现为数组字段,您可以使用 afArrayField 组件:

<template name="editPlayer">
    {{#autoForm id="editplayer" collection="Player" type="update" doc=currentPlayer}}
        {{> afQuickField name="name"}}
        {{> afArrayField name="country"}}
        <button type="submit">Submit</button>
    {{/autoForm}}
</template>

if (Meteor.isClient) {
    AutoForm.debug();
    Template.editPlayer.helpers({
        currentPlayer: function () {
            return Player.findOne();
        }
    });
}

if (Meteor.isServer) {
    Meteor.startup(function () {
        let country = {slug: 'austria', name: 'Austria'};
        Country.insert(country);
        Player.insert({name: 'Matthias Eckhart', country: [country]});
    });
}

Player = new Mongo.Collection("player");
Country = new Mongo.Collection("country");

CountrySchema = new SimpleSchema({
    slug: {
        type: String,
        max: 100,
        index: true
    },
    name: {
        type: String,
        max: 100
    }
});

Player.attachSchema(new SimpleSchema({
    name: {
        type: String,
        label: "Player name",
        index: true
    },
    country: {
        type: [CountrySchema],
        label: "country",
        max: 5,
        index: true,
        autoform: {
            options: function () {
                return Country.find().map((object) => ({label: object.name, value: object._id}));
            }
        }
    }
}));

请注意,我假定您要更新 player 文档。因此,我设置了属性 doc=currentPlayer 并通过 currentPlayer 辅助函数指定了一个 player 文档。如果您设置数据上下文,例如通过 data function in your Iron Router 路由,您可以使用 doc=this.


如果您想要一个简单的 select 表单,您当前的数据结构可能需要 解决方法 。问题是 [CountrySchema] 没有扩展为正确的模式键。因此,您需要明确指定完整模式并使用 AutoForm hooks 和 Meteor 辅助函数 (currentPlayer):

转换 selected 选项
<template name="editPlayer">
    {{#autoForm id="editplayer"  collection="Player" type="update" doc=currentPlayer}}
        {{> afQuickField name="name"}}
        {{> afQuickField name="country"}}
        <button type="submit">Submit</button>
    {{/autoForm}}
</template>

if (Meteor.isClient) {
    AutoForm.debug();
    Template.editPlayer.helpers({
        currentPlayer: function () {
            let player = Player.findOne();
            if (player) player.country = _.map(player.country, (country) => country._id);
            return player;
        }
    });
    var playerUpdateHook = {
        before: {
            update: function (doc) {
                doc['$set'].country = _.map(doc['$set'].country, (countryId) => Country.findOne({_id: countryId}));
                return doc;
            }
        }
    };
    AutoForm.addHooks('editplayer', playerUpdateHook);
}

if (Meteor.isServer) {
    Meteor.startup(function () {
        let country = {slug: 'austria', name: 'Austria'};
        let countryId = Country.insert(country);
        country = Country.findOne({_id: countryId});
        Player.insert({name: 'Matthias Eckhart', country: [country]});
    });
}

Player = new Mongo.Collection("player");
Country = new Mongo.Collection("country");

CountrySchema = new SimpleSchema({
    _id: {
        type: String,
        index: true
    },
    slug: {
        type: String,
        max: 100,
        index: true
    },
    name: {
        type: String,
        max: 100
    }
});

Player.attachSchema(new SimpleSchema({
    name: {
        type: String,
        label: "Player name",
        index: true
    },
    country: {
        type: [CountrySchema],
        label: "country",
        max: 5,
        index: true
    },
    'country.$': {
        type: String,
        autoform: {
            options: function () {
                return Country.find().map((object) => ({label: object.name, value: object._id}));
            }
        }
    }
}));