【发布时间】:2016-04-17 20:43:52
【问题描述】:
我想使用 autoform 将一个新文档插入到 db 中。 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 因此,我相信不需要从客户端或在流星方法中进行插入。
所以带有架构的集合看起来像这样:
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 不。它不是模态的。
-
嗯。我会在此过程中添加一些控制台日志,以查看正确调用的内容。
-
我在方法调用中添加了一个控制台日志,以了解它何时被触发。它没有。哦,还有那个模态类,它关闭由其他东西引起的模态动作。
-
您是否使用“保存”按钮提交?尝试添加
type="submit"。