【发布时间】:2015-09-09 21:57:06
【问题描述】:
我是流星和 autoform 的新手,我正在尝试将表单插入到 mongo 中。无论我改变了什么,它都行不通。我不知道接下来要尝试什么。
我已删除不安全和自动发布。附件是我的 .js 和 html 文件的链接。
我已经设置了一个方案,让表单完美地显示在 html 上。现在当我点击提交时,没有任何东西被放入mongo。我有允许规则设置。我有大量的 console.logs 显示,它们都触发并跟随,就好像帖子成功了一样。事实上,在 onSuccess 中,我得到了一个文档编号,但我的数据库中没有任何内容。
非常感谢这里的任何帮助。我知道这一定是小事,但我已经绞尽脑汁进行了数小时无休止的搜索。
.js 文件没有方案。 plnkr链接中的完整js文件
if (Meteor.isClient) {
// ********************************************************
// *** Creating the database scheme for the customer account
// ********************************************************
customers = new Mongo.Collection("customer");
AutoForm.debug();
var postHooks = {
before: {
insert: function(doc) {
console.log("Getting to posting hooks");
if(Meteor.userId()){
doc.createdUser = Meteor.userId();
doc.createdDate = Date();
console.log("Got to the insert before hook!");
}
return doc;
}
},
after: {
// Replace `formType` with the form `type` attribute to which this hook applies
insert: function(error, result) {
console.log("Getting to the after insert function");
console.log(error);
console.log(result);
console.log("New Document ID is " + this.docId);
}
},
onSuccess: function(formType, result) {
console.log("Getting to the insert sucess area");
console.log(result);
},
onError: function(formType, error) {
console.log(error);
}
}
AutoForm.addHooks('insertCustomer', postHooks);
Template.customerTemplate.helpers({
showLoginError: function(){
return showCustomerSaveError;
}
});
}
if (Meteor.isServer) {
customers = new Mongo.Collection("customer");
customers.allow({
insert: function (userId, doc) {
console.log("Getting to the insert server call");
// the user must be logged in
return !! userId;
},
update: function (userId, doc, fields, modifier) {
// can only change your own documents
return doc.owner === userId;
},
remove: function (userId, doc) {
// can only remove your own documents
return doc.owner === userId;
},
fetch: ['owner']
});
}
【问题讨论】:
-
我没有读过这个问题,但我建议你使用 Autoform.debug()。这会将 autoform 错误打印到控制台并更容易解决您的问题。
-
我已经在代码中列出了它,它只将一件事打印到控制台,这是日期函数的一种干净方法。调试不会产生其他语句或消息。
-
您确定数据库中没有任何内容吗?可能是您没有发布文档,它确实在数据库中。向我们展示您的日志也可能有助于调试
-
是的。我将不安全的包重新打开并运行 db find().count 并返回 0。同样使用 Mongol,它也显示 0 条记录
-
您一般不想使用自动发布。但是,您希望将文档发布给客户。您的文档已添加到数据库中,您只是在客户端看不到它,因为服务器尚未将其发送到客户端。
标签: javascript mongodb meteor meteor-autoform