简体   繁体   English

除非设置{validate:false},否则SimpleSchema插入将失败并显示无用的错误

[英]SimpleSchema insert fails with unhelpful error unless {validate: false} is set

At this point I've actually pared down my code to mimic the example in the docs , and I'm getting the same error. 至此,我实际上已经缩减了代码以模仿docs中示例 ,并且遇到了同样的错误。

I've defined my Collection as such: 我已经这样定义了我的收藏:

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

The autovalue stuff is borrowed straight from the docs. autovalue东西是直接从文档中借用的。

And have a method for calling insert: 并有一个调用insert的方法:

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

        Categories.insert({title: title}, (err, res) => {
            if(err){
                console.error("Category insert failed: " + err);
            }
        });
    },
...

As I've tried to get through this issue, I've added and removed rules to the schema definition, sometimes getting a call stack exceeded error. 在尝试解决此问题时,我已在规则定义中添加和删除了规则,有时会导致call stack exceeded错误。 Mostly what I get is the following: 我得到的大部分是以下内容:

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]

EDIT 编辑

After swapping out the arrow functions: 换出箭头功能后:

methods.js methods.js

Meteor.methods({
    // Settings: create new category
    'categories.insert': function(title){
        check(title, String);

        Categories.insert({title: title}, function(err, res){
            if(err){
                console.error("Category insert failed: " + err);
            }
        });
    },
...

settings.js 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 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);

Still getting the same error as in original post 仍然收到与原始帖子相同的错误

You're using arrow functions like Sean mentioned in the comment. 您正在使用箭头函数,如评论中提到的Sean。 Replace them with traditional function(){...} and you should be good to go. 用传统的function(){...}代替它们,您应该会很好。

You can read more about this issue here 您可以在此处阅读有关此问题的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM