Meteor 除非设置了{validate:false},否则SimpleSchema插入将失败,并出现无效错误

Meteor 除非设置了{validate:false},否则SimpleSchema插入将失败,并出现无效错误,meteor,simple-schema,meteor-collection2,Meteor,Simple Schema,Meteor Collection2,在这一点上,我实际上已经减少了我的代码来模拟,我得到了相同的错误 我将我的收藏定义为: var Categories = new Mongo.Collection('categories'); const CategorySchema = new SimpleSchema({ title: { type: String, label: "Title", max: 255, optional: true },

在这一点上,我实际上已经减少了我的代码来模拟,我得到了相同的错误

我将我的收藏定义为:

var Categories = new Mongo.Collection('categories');

const CategorySchema = new SimpleSchema({
    title: {
        type: String,
        label: "Title",
        max: 255,
        optional: true
    },
    created: {
        type: Date,
        label: "Date Category Created",
        autoValue: () => {
            if(this.isInsert){
                return new Date();
            } else if (this.isUpsert) {
                return {$setOnInsert: new Date()};
            }
        }
    },
    updated: {
        type: Date,
        label: "Date Category Modified",
        autoValue: () => {
            if(this.isUpdate){
                return new Date();
            }
        },
        optional: true
    }
});

Categories.attachSchema(CategorySchema);

export { Categories };

并具有调用insert的方法:

Meteor.methods({
    'categories.insert'(title){
        check(title, String);

        Categories.insert({title: title}, (err, res) => {
            if(err){
                console.error("Category insert failed: " + err);
            }
        });
    },
...
当我试图解决这个问题时,我向模式定义中添加和删除了规则,有时会出现
调用堆栈超出
的错误。我得到的主要信息如下:

Exception while simulating the effect of invoking 'categories.insert' TypeError: func is not a function
    at http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:13027:18
    at Array.forEach (native)
    at doValidation (http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:13026:17)
    at ValidationContext.validate (http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:12580:57)
    at ns.Collection.doValidate (http://localhost:3000/packages/aldeed_collection2-core.js?hash=0564817159bbe9f8209b6a192a1fa2b2bbab924a:466:33)
    at ns.Collection.Mongo.Collection.(anonymous function) [as insert] (http://localhost:3000/packages/aldeed_collection2-core.js?hash=0564817159bbe9f8209b6a192a1fa2b2bbab924a:260:25)
    at ns.Collection.<anonymous> (http://localhost:3000/packages/matb33_collection-hooks.js?hash=d44fd0eb02806747e1bb0bdc3938463e415c63ec:402:19)
    at ns.Collection.collection.(anonymous function) [as insert] (http://localhost:3000/packages/matb33_collection-hooks.js?hash=d44fd0eb02806747e1bb0bdc3938463e415c63ec:155:21)
    at DDPCommon.MethodInvocation.categories.insert (http://localhost:3000/app/app.js?hash=580bf82f2f196202280f32cf5ffacbc930cd6b10:20073:14)
    at http://localhost:3000/packages/ddp-client.js?hash=c9ca22092a3137a7096e8ab722ba5ab8eedb9aec:4076:25 TypeError: func is not a function
    at http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:13027:18
    at Array.forEach (native)
    at doValidation (http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:13026:17)
    at ValidationContext.validate (http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:12580:57)
    at ns.Collection.doValidate (http://localhost:3000/packages/aldeed_collection2-core.js?hash=0564817159bbe9f8209b6a192a1fa2b2bbab924a:466:33)
    at ns.Collection.Mongo.Collection.(anonymous function) [as insert] (http://localhost:3000/packages/aldeed_collection2-core.js?hash=0564817159bbe9f8209b6a192a1fa2b2bbab924a:260:25)
    at ns.Collection.<anonymous> (http://localhost:3000/packages/matb33_collection-hooks.js?hash=d44fd0eb02806747e1bb0bdc3938463e415c63ec:402:19)
    at ns.Collection.collection.(anonymous function) [as insert] (http://localhost:3000/packages/matb33_collection-hooks.js?hash=d44fd0eb02806747e1bb0bdc3938463e415c63ec:155:21)
    at DDPCommon.MethodInvocation.categories.insert (http://localhost:3000/app/app.js?hash=580bf82f2f196202280f32cf5ffacbc930cd6b10:20073:14)
    at http://localhost:3000/packages/ddp-client.js?hash=c9ca22092a3137a7096e8ab722ba5ab8eedb9aec:4076:25
meteor.js?hash=27829e9…:930 Error invoking Method 'categories.insert': Internal server error [500]
settings.js

Template.settings.onCreated(function(){
    Meteor.subscribe('categories');
});

Template.settings.helpers({
    categories: function(){
        return Categories.find();
    }
});

Template.settings.events({
    'click #newCategoryButton': function(e){
        let title = $("#newCategoryInput").val();
        if(title !== ""){
            Meteor.call('categories.insert', title);
            $("#newCategoryInput").val("").focus();
        }
    },
    'click .removeCategory': function(e){
        var id = $(e.currentTarget).data('id');
        Meteor.call('categories.remove', id);
    }
});
Categories.js

var Categories = new Mongo.Collection('categories', options);

const CategorySchema = new SimpleSchema({
    title: {
        type: String,
        label: "Title",
        max: 255,
        optional: true
    },
    created: {
        type: Date,
        label: "Date Category Created",
        autoValue: function(){
            if(this.isInsert){
                return new Date();
            } else if (this.isUpsert) {
                return {$setOnInsert: new Date()};
            }
        }
    },
    updated: {
        type: Date,
        label: "Date Category Modified",
        autoValue: function(){
            if(this.isUpdate){
                return new Date();
            }
        },
        optional: true
    }
});

Categories.attachSchema(CategorySchema);

仍然得到与原始帖子中相同的错误

您正在使用评论中提到的Sean之类的箭头函数。用传统的
function(){…}
替换它们,您就可以开始了


您可以阅读有关此问题的更多信息

尝试用普通函数声明替换箭头函数-看看这是否会有所不同我真的希望这能奏效,但运气不好。我删除了箭头函数,甚至用单词“function”替换了对象中function的es6语法。同样的错误:(好的,你能用你尝试过的变体更新你的原始问题吗?上面是编辑过的代码。我有可能自己写验证吗?
var Categories = new Mongo.Collection('categories', options);

const CategorySchema = new SimpleSchema({
    title: {
        type: String,
        label: "Title",
        max: 255,
        optional: true
    },
    created: {
        type: Date,
        label: "Date Category Created",
        autoValue: function(){
            if(this.isInsert){
                return new Date();
            } else if (this.isUpsert) {
                return {$setOnInsert: new Date()};
            }
        }
    },
    updated: {
        type: Date,
        label: "Date Category Modified",
        autoValue: function(){
            if(this.isUpdate){
                return new Date();
            }
        },
        optional: true
    }
});

Categories.attachSchema(CategorySchema);