我想使用autoform將新文檔插入到db中。 Autoform鉤子在服務器上調用流星方法來插入文檔。通過autoform鉤子插入新文檔調用流星方法
我有這個模板...
{{#autoForm collection="Reports" id="addReport" type="insert"}}
<div class="row">
<div class="col s6">
{{> afQuickField name='hours'}}
</div>
</div>
<button class="btn waves-effect waves-light modal-action modal-close"><i class="material-icons">save</i></button>
{{/autoForm}}
則...
AutoForm.hooks({
addReport: {
onSubmit: function(insertDoc) {
Meteor.call('addReport', insertDoc, function(error, result) {
if (error) alert(error.reason);
});
return false;
}
}
});
然後在服務器上的方法...
Meteor.methods({
addReport: function(insertDoc) {
var report = _.extend(insertDoc, {
userId: Meteor.userId(),
});
return Reports.insert(report);
}
});
我有一個createdAt
和updatedAt
集合中的字段,但它們都具有autoValue,因此我相信不需要插入fr om客戶端或流星法。
所以收集與架構是這樣的:
Reports = new Meteor.Collection('reports');
Reports.attachSchema(new SimpleSchema({
hours: {
type: Number,
label: "Number of hours",
decimal: true
},
createdAt: {
type: Date,
label: "Created Date",
autoValue: function() {
if (this.isInsert) {
return new Date;
} else {
this.unset();
}
},
denyUpdate: true
},
updatedAt: {
type: Date,
autoValue: function() {
if (this.isUpdate) {
return new Date()
}
},
denyInsert: true,
optional: true
},
"userId": {
type: String,
autoform: {
type: "hidden",
}
},
}));
當我運行流星,形式顯示,而提交什麼都不做。沒有視覺提示如果有任何錯誤。在客戶端和服務器控制檯中都沒有錯誤消息。
我在做什麼錯誤或失蹤?
這是模式嗎?它是否在模態之外工作? –
@AutumnLeonard nope。它不在一個模式。 – Rexford
嗯。我會一路添加一些控制檯日誌以查看正確調用的內容。 –