Как проверить обновление по SimpleSchema перед обновлением документа в коллекции

Я пытаюсь проверить свои данные по SimpleSchema, прежде чем они будут отправлены в коллекцию, но по какой-то причине я не могу сделать это с этой ошибкой.

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

У меня есть одна коллекция с двумя SimpleSchemas следующим образом.

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?

Любая помощь будет принята с благодарностью.


person BaconJuice    schedule 03.01.2016    source источник


Ответы (1)


Поскольку вы уже определили подсхему для подобъекта, вы можете напрямую проверить это:

check(vendorCategories,VendorCategoriesSchema)
person Michel Floyd    schedule 03.01.2016