简体   繁体   English

使用流星方法将表单数据保存到集合中

[英]Save form data to collection using meteor methods

Using Meteor I'm trying to capture and save some data (name, email and age) using a form. 我正在使用Meteor尝试使用表单捕获并保存一些数据(姓名,电子邮件和年龄)。 This data should be saved in a new Meteor collection "Subscribers". 此数据应保存在新的Meteor集合“订户”中。 My code as follows: 我的代码如下:

Template Events (client\\views\\subscribe_form\\subscribe_form.js) 模板事件 (client \\ views \\ subscribe_form \\ subscribe_form.js)

Template.Subscribe.events({ 
    'submit form#subscribe-form': function(event){
        // Prevent default browser form submit
        event.preventDefault();

        // Get values from the form
        var subName = $('form#subscribe-form [name=subscribe-name]').val();
        var subEmail = $('form#subscribe-form [name=subscribe-email]').val();
        var subAge = $('form#subscribe-form [name=subscribe-age]').val();

        let subscriberData = {
            name: subName,
            email: subEmail,
            age: subAge,
            createdAt: new Date()
        };

        // Insert subscriber into the collection
        Meteor.call('SubscribeNow', subscriberData, function(error, result){
            if(error){
                // Output error if subscription fails
                console.log(error.reason);
            } else {
                // Success
                console.log("Subscription successful");
                console.log(subscriberData);
                console.log( Subscribers.find() );
            }
        });
    },
});

Server side (server\\collections\\subscribers.js) 服务器端 (server \\ collections \\ subscribers.js)

var Subscribers = new Meteor.Collection('subscribers');

Subscribers.allow({
    insert: function(){
        return true;
    }
});

Meteor.methods({
    'SubscribeNow': function (subscriberData) {
        //check(subscriberData, String);

        try {
            // Any security checks, such as logged-in user, validating data, etc.
            Subscribers.insert(subscriberData);
        } catch (error) {
            // error handling, just throw an error from here and handle it on client
            if (badThing) {
                throw new Meteor.Error('bad-thing', 'A bad thing happened.');
            }
        }
    }
});

Now when I add some data to the form and click the submit button it goes through the success console.log message, the data is picked-up properly but whenever I try to query the collection it won't show anything at all. 现在,当我向表单添加一些数据并单击“提交”按钮时,它会通过成功console.log消息,可以正确提取数据,但是每当我尝试查询集合时,它都不会显示任何内容。

I tried looking for the data in the collection using a simple template I created to list the Subscribers collection in a table, also with Meteor Toys and via console.log( Subscribers.find() ); 我尝试使用创建的简单模板在表中查找集合中的数据,该模板还通过Meteor Toys以及通过console.log( Subscribers.find() );在表中列出Subscribers集合console.log( Subscribers.find() ); but no luck. 但没有运气。 It seems the forms goes through but the data is not saved in the collection. 似乎表格通过了,但是数据没有保存在集合中。

在此处输入图片说明

Also, autopublish and insecure are removed. 同样, 自动发布不安全也将被删除。

在此处输入图片说明

What am I doing wrong? 我究竟做错了什么? I'm still pretty new to everything Meteor so it might be something obvious I'm missing here. 我对Meteor的一切仍然很陌生,因此可能很明显我在这里不见了。

Let me know if you need to see more of the code. 让我知道是否需要查看更多代码。 Finally, any suggestions for code improvement (structuring or such) are welcome. 最后,欢迎提供任何有关代码改进的建议(结构化等)。

Many thanks in advance! 提前谢谢了!

So from your question and comments you've added to your question, the Subscribers collection data is being saved properly (you've verified this using meteor mongo ), but you can't retrieve the data using Subscribers.find() . 因此,根据您的问题和添加到问题中的注释, Subscribers集合数据已正确保存(您已使用meteor mongo进行了验证),但无法使用Subscribers.find()检索数据。 Since you've removed the autopublish package, you'll have to make sure you're subscribing to a publication that's responsible for pushing your Subscribers data from the server to the client. 由于已删除autopublish程序包,因此必须确保您正在订阅负责将Subscribers数据从服务器推送到客户端的发布。 For example: 例如:

/server/publications.js /server/publications.js

Meteor.publish('allSubscribers', function () {
  return Subscribers.find();
});

/client/some_template.js /client/some_template.js

Template.someTemplate.onCreated(function () {
  this.subscribe('allSubscribers');
});
...

After you've subscribed to your data, you can then run Subscribers.find() client side, and have data returned. 订阅数据后,可以运行Subscribers.find()客户端,并返回数据。

For more information, see the Publications and Data Loading section of the Meteor Guide. 有关更多信息,请参见《流星指南》的“ 出版物和数据加载”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM