簡體   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