2016-01-03 2 views
0

Я пытаюсь проверить мои данные на SimpleSchema, прежде чем он будет отправлен в коллекцию, но по какой-то причине я не могу это сделать с этой ошибкой.Как проверить обновление до 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 будет добавлена ​​после создания документа поставщика.

Вот как выглядит моя клиентская сторона.

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? 

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

ответ

1

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

check(vendorCategories,VendorCategoriesSchema) 
Смежные вопросы