简体   繁体   中英

Verifying schema on Meteor Method using autoform

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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