Javascript 使用autoform验证Meteor方法的模式
我用的是自动表格,集合2。我想对插入/更新使用方法调用类型,因为我想在保存到服务器中的数据库之前添加其他字段。SimpleSchema将检查客户机中的数据,但如何使数据也根据服务器端的模式进行检查?我添加新数据的方法如下:Javascript 使用autoform验证Meteor方法的模式,javascript,meteor,meteor-autoform,meteor-collection2,Javascript,Meteor,Meteor Autoform,Meteor Collection2,我用的是自动表格,集合2。我想对插入/更新使用方法调用类型,因为我想在保存到服务器中的数据库之前添加其他字段。SimpleSchema将检查客户机中的数据,但如何使数据也根据服务器端的模式进行检查?我添加新数据的方法如下: Meteor.methods({ companyAdd: function (companyAttr) { // add additional fields to document var currentDate = new Date();
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的文档中发现,如果以后还有其他人需要解决方案:您只需检查模式:
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};
}
}
你能接受你自己的答案并发布一个更简洁的代码吗?您的代码中充满了小细节和不必要的调整来理解问题。thx对于警告,为了便于理解,我清除了一些代码