【问题标题】:Meteor - Add an object from a select form to a collectionMeteor - 将选择表单中的对象添加到集合中
【发布时间】:2016-09-16 08:20:33
【问题描述】:

我正在尝试将类别添加到我的帖子中,因此我有一个带有选择框的表单,用户可以在其中选择要与他的帖子关联的类别。问题是,当我提交表单时,出现错误,因为字段 Category 不是对象...我尝试将表单与 formToDoc 挂钩以修改 doc 并使类别元素看起来不错,但它不起作用。我猜我必须处理我的数据验证,但我不知道怎么做?

这是我的收藏 CPDM 和类别:

Category = new Mongo.Collection("category");

// Création du schéma des catégories
Category.attachSchema(new SimpleSchema({
    name: {
        type: String,
        label: "Catégorie",
        max: 200
    },
    value: {
        type: String,
        label: "Catégorie Value",
        max: 200
    }
}));

// Création du schéma des CPDM
CPDM.attachSchema(new SimpleSchema({
    title: {
        type: String,
        label: "Titre",
        max: 200
    },
    content: {
        type: String,
        label: "Contenu",
        autoform: {
            afFieldInput: {
                type: "textarea",
                rows: 15
            }
        }
    },
    createdAt: {
        type: Date,
        autoform: {
            omit: true
        },
        autoValue: function() {
            if (this.isInsert) {
                return new Date;
            }
            else {
                this.unset();
            }
        }
    },
    author: {
        type: String,
        autoform: {
            omit: true
        },
        autoValue: function () {
            if (this.isInsert) {
                if (Meteor.user()) {
                    return Meteor.user().username;
                } else {
                    return "Anonyme";
                }
            } else {
                this.unset();
            }
        }
    },
    ranking: {
        type: Number,
        autoValue: function () {
            if (this.isInsert) {
                return 0;
            }
        },
        autoform: {
            omit: true
        },
        min: 0,
        label: "Note"
    },
    voters: {
        type: [String],
        autoform: {
            omit: true
        },
        autoValue: function () {
            if (this.isInsert) {
                return [];
            }
        }
    },
    selected: {
        type: Boolean,
        autoform: {
            omit: true
        },
        autoValue: function () {
            if (this.isInsert) {
                return false;
            }
        }
    },
    category: {
        type: Object,
        optional: true,
        label: "Catégorie",
        autoform: {
            firstOption: "Sélectionner une catégorie"
        }
    }
}));

这是我的 formHook :

AutoForm.hooks({
    createCPDM: { // ID du formulaire
        formToDoc: function(doc) {
            var categoryName = $("[name='category'] option:selected").text();
            doc.category = {name:categoryName, value:doc.category};
            return doc;
        },
        onSubmit: function (doc) { // Gestion du formulaire de soumission
            var error = null;
            var title = doc.title;
            var content = doc.content;
            var category = doc.category;
            var captcha = $("#captcha").val();

            var formData = {
                title: title,
                content: content,
                category: category
            };

            if (captcha == 4) {
                Meteor.call('createCPDM', formData, function (err) {
                    if (err) {
                        error = new Error("Une erreur s'est produite");
                    }
                });
            }
            else {
                error = new Error("Mauvais captcha");
            }

            if (error === null) {
                this.done(); // Appelle onSuccess
            }
            else {
                this.done(error); // Appelle onError
            }

            return false;
        },

感谢您的帮助!

【问题讨论】:

    标签: javascript forms mongodb meteor collections


    【解决方案1】:

    我找到了解决方案,我必须调整我的 Post 集合以使 category 成为一个对象。像这样:

    category: {
            type: Object,
            optional: true,
            label: "Catégorie",
            autoform: {
                firstOption: "Sélectionner une catégorie"
            }
        },
        "category.name": {
            type: String,
            label: "Catégorie",
            max: 200,
            autoform: {
                omit: true
            }
        },
        "category.value": {
            type: String,
            label: "Catégorie Value",
            max: 200,
            autoform: {
                omit: true
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      相关资源
      最近更新 更多