Meteor AutoForm: how to update schema values ​​with an array of subdocuments?

I am new to Meteor AutoForm. I want to update documents with a country documenter. Below is my code. If I add {{> afQuickField name="country"}}to AutoForm, this will not work. {{> afQuickField name="name"}}works great. How to add whole country documents to the players country (_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}}

I added a SimpleSchema.debug = true;console log SimpleSchema invalid keys for "editplayer" context.

+4
source share
1 answer

If you want to display countryas an array field, you can use the component 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 player currentPlayer. , , data Iron Router, doc=this.


, . , [CountrySchema] . , "" "" (currentPlayer):

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

Source: https://habr.com/ru/post/1611260/


All Articles