【问题标题】:Meteor: Autoform not submitting posts to mongoMeteor:Autoform 不向 mongo 提交帖子
【发布时间】: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']
    });

}

http://plnkr.co/edit/5pM0Co9luGLIBBNKP5YA

【问题讨论】:

  • 我没有读过这个问题,但我建议你使用 Autoform.debug()。这会将 autoform 错误打印到控制台并更容易解决您的问题。
  • 我已经在代码中列出了它,它只将一件事打印到控制台,这是日期函数的一种干净方法。调试不会产生其他语句或消息。
  • 您确定数据库中没有任何内容吗?可能是您没有发布文档,它确实在数据库中。向我们展示您的日志也可能有助于调试
  • 是的。我将不安全的包重新打开并运行 db find().count 并返回 0。同样使用 Mongol,它也显示 0 条记录
  • 您一般不想使用自动发布。但是,您希望将文档发布给客户。您的文档已添加到数据库中,您只是在客户端看不到它,因为服务器尚未将其发送到客户端。

标签: javascript mongodb meteor meteor-autoform


【解决方案1】:

您正在将文件添加到数据库中,但没有发布它。 运行 meteor add autopublish 或签入 MongoDB 本身以查看文档。

【讨论】:

  • 这和上面的 cmets 帮助我弄清楚我没有使用发布和订阅方法。感谢您的帮助。
猜你喜欢
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 2018-04-29
  • 1970-01-01
  • 2011-04-20
  • 2023-03-04
  • 2017-11-13
相关资源
最近更新 更多