繁体   English   中英

使用自动套准在流星方法上验证架构

[英]Verifying schema on Meteor Method using autoform

我正在使用autoform,collection2。 我想使用方法调用类型进行插入/更新,因为我想在保存到服务器中的数据库之前添加其他字段。 SimpleSchema会检查客户端中的数据,但是如何使数据也与服务器端的模式相对应? 我添加新数据的方法如下:

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

我在simpleschema的文档中找到了,如果以后还有其他人需要解决方案:您可以对照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};
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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