Meteor - 将选定表单中的对象添加到集合中

时间:2022-01-15 19:39:55

I'm trying to add a Category to my Post, so I have a form with a select box where the user can choose the category to associate with his post. The problem is that when I submit my form, I had an error because the field Category is not an object... I tried to hook the form with formToDoc to modify the doc and make the category element look good but it doesn't work. I'm guessing that I have to deal with the validation of my data but I don't know how ?

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

Here are my collections CPDM and Category :

以下是我的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"
        }
    }
}));

And here is my 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;
        },

Thank's for helping !

谢谢你的帮助!

1 个解决方案

#1


0  

I found the solution, I had to adjust my Post collection to make category to be an object. Like this :

我找到了解决方案,我不得不调整我的Post集合,使类别成为一个对象。像这样 :

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
        }
    }

#1


0  

I found the solution, I had to adjust my Post collection to make category to be an object. Like this :

我找到了解决方案,我不得不调整我的Post集合,使类别成为一个对象。像这样 :

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
        }
    }