0

我正在尝试在将数据提交到集合之前针对 SimpleSchema 验证我的数据,但由于某种原因,我无法使用此错误来执行此操作。

Exception while invoking method 'createVendorCategory' { stack: 'TypeError: Cannot call method \'simpleSchema\' of undefined

我有一个包含两个 SimpleSchema 的集合,如下所示。

Vendors = new Mongo.Collection('vendors'); //Define the collection.

VendorCategoriesSchema = new SimpleSchema({
    name: {
        type: String,
        label: "Category"
    },
    slug: {
        type: String,
        label: "Slug"
    },
    createdAt : {
        type: Date,
        label: "Created At",
        autoValue: function(){
            return new Date()//return the current date timestamp to the schema
        }
    }
});

VendorSchema = new SimpleSchema({
    name: {
        type: String,
        label: "Name"
    },
    phone: {
        type: String,
        label: "Phone"
    },
    vendorCategories:{
        type: [VendorCategoriesSchema],
        optional: true
    }
});
Vendors.attachSchema(VendorSchema);

vendorCategory 将在用户创建 Vendor 文档后添加。

这是我的客户端的样子。

Template.addCategory.events({
    'click #app-vendor-category-submit': function(e,t){
        var category = {
            vendorID: Session.get("currentViewingVendor"),
            name: $.trim(t.find('#app-cat-name').value),
            slug: $.trim(t.find('#app-cat-slug').value),
        };

        Meteor.call('createVendorCategory', category, function(error) {
            //Server-side validation
            if (error) {
                alert(error);
            }
        });
    }
});

这是我的服务器端 Meteor.methods 的样子

createVendorCategory: function(category) 
{ 
    var vendorID =  Vendors.findOne(category.vendorID);
    if(!vendorID){
        throw new Meteor.Error(403, 'This Vendor is not found!');
    }
    //build the arr to be passed to collection
    var vendorCategories = {
        name: category.name,
        slug: category.slug
    }

    var isValid = check( vendorCategories, VendorSchema.vendorCategories.simpleSchema());//This is not working?
    if(isValid){
        Vendors.update(VendorID, vendorCategories);
        // return vendorReview;
        console.log(vendorCategories);
    }
    else{
        throw new Meteor.Error(403, 'Data is not valid');
    }
}

我猜这就是错误的来源。

var isValid = check( vendorCategories, VendorSchema.vendorCategories.simpleSchema());//This is not working?

任何帮助将不胜感激。

4

1 回答 1

1

由于您已经为子对象定义了子架构,您可以直接检查:

check(vendorCategories,VendorCategoriesSchema)
于 2016-01-03T19:44:12.323 回答