5

I'm using autoform, collection2. I want to use method call type for insert/update, as I want to add additional fields before saving to database in server. SimpleSchema would check the data in client, but how can I make the data is checked against the schema on server-side as well? My method for adding new data is as follows:

Meteor.methods({
  companyAdd: function (companyAttr) {

    // add additional fields to document

    var currentDate = new Date(); 

    var company = _.extend(companyAttr, {
        createdBy: user._id,
        createdAt: currentDate
    });

    var newCompanyId = Companies.insert(company);
    return {_id: newCompanyId};
  }
}
Ahmet Cetin
  • 3,683
  • 3
  • 25
  • 34
  • Could you please accept your own answer and post a more concise code? Your code is full of small details and tweaks not necessary to understand the question. – Kyll Mar 10 '15 at 15:53
  • thx for the warning, i cleared the code a bit for easy understanding – Ahmet Cetin Mar 11 '15 at 20:40

1 Answers1

5

I found in documentation of simpleschema, if anyone else would need solution later on: you can just check against schema:

Meteor.methods({
   companyAdd: function (companyAttr) {

   //here we check the data sent to method against the defined schema
   check(companyAttr, Companies.simpleSchema());

   var currentDate = new Date(); 

   var company = _.extend(companyAttr, {
      createdBy: user._id,
      createdAt: currentDate
   });

   var newCompanyId = Companies.insert(company);
   return {_id: newCompanyId};
  }
}
Ahmet Cetin
  • 3,683
  • 3
  • 25
  • 34