Meteor异步的基本概念:提交电子邮件并发送电子邮件

Basic Meteor Asynchronous Concept: Submit an Email and Send Email

本文关键字:电子邮件 并发 提交 异步 基本概念 Meteor      更新时间:2024-02-07

我对Meteor框架真的很陌生,很难理解它的概念,它来自Meteor的传统响应和请求后台(NodeJS异步回调风格)。

我只是想建立一个登陆页面,告诉我你的名字和电子邮件地址。用户点击提交后,我希望将用户保存在数据库(MongoDB)中,并向Mandrill端点发送请求,以便Mandrill可以向该特定电子邮件地址发送电子邮件。

我在传统回应和请求中的做法是这样的。用户提交他们的姓名和电子邮件地址,这将是对我的服务器的POST请求,在我的服务器上,我的ORM将把它保存到数据库中,回调成功后,我向Mandrill发出请求,向该特定用户发送电子邮件。

我在Meteor中的代码现在如下:

Template.welcome.events({
  'submit form': function(e) {
    e.preventDefault();
    var subscribe = {
      name: $(e.target).find('[name="name"]').val(),
      email: $(e.target).find('[name="email"]').val()
    };
    var errors = validateSubscribes(subscribe);
    if (Object.keys(errors).length > 0) {
      for (var type in errors) {
        toast(errors[type], 2000);
      }
      return;
    }
    Subscribes.insert(subscribe, function(error, result) {
      if (error)
        return toast("Oops, something is wrong, try again");
      if (result) {
        $(e.target).find('button:submit')
                   .attr("disabled", "disabled");
        $(e.target).find('[name="name"]')
                   .val("")
                   .attr("disabled", "disabled");
        $(e.target).find('[name="email"]')
                   .val("")
                   .attr("disabled", "disabled");
        return toast('Thank you for subscribing!', 3000);
      }
    });
  }
});

我的Mandrill代码如下(来自https://atmospherejs.com/wylio/mandrill)

#server code
Meteor.Mandrill.sendTemplate({
    "key": "YOUR_MANDRILL_API_KEY", // optional, if you set it in with Meteor.Mandril.config() already
    "template_name": "YOUR_TEMPLATE_SLUG_NAME",
    "template_content": [
      {}
    ],
    "message": {
        "global_merge_vars": [
            {
                "name": "var1",
                "content": "Global Value 1"
            }
        ],
        "merge_vars": [
            {
                "rcpt": "email@example.com",
                "vars": [
                    {
                        "name": "fname",
                        "content": "John"
                    },
                    {
                        "name": "lname",
                        "content": "Smith"
                    }
                ]
            }
        ],
        "to": [
        {"email": email@example.com}
        ]
    }
});

此外,在集合上,我允许订阅

Subscribes = new Mongo.Collection('subscribes');
Subscribes.allow({
  insert: function(userId, subscribe) { return true; }
});
validateSubscribes = function(subscribe) {
  var errors = {},
      regExp = /^(([^<>()[']''.,;:'s@'"]+('.[^<>()[']''.,;:'s@'"]+)*)|('".+'"))@(('[[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'.[0-9]{1,3}'])|(([a-zA-Z'-0-9]+'.)+[a-zA-Z]{2,}))$/;
   if (!subscribe.name)
    errors.name = 'Please fill in a name';
  if (!subscribe.email || !regExp.test(subscribe.email))
    errors.email = 'Please fill in a valid email';
  return errors;
};

我有三个问题:

  • 为什么使用allow?为什么不使用Meteor.method和Meteor.call
  • 我把山卓代码放在哪里?回拨内部?我觉得流星风格的节目应该是同步风格吗
  • 我可以把我所有的验证逻辑放在这里,把我所有jQueryUI逻辑放在这个提交表单事件点击中。我可以看到它很快就会膨胀。有什么具体的方法来组织所有这些吗

非常感谢你的回答。流星是超级酷的,只需要我的头围绕它一点点。

Christian

  1. 我们中的许多人只使用Meteor.Method。请参阅Discover Meteor博客,了解一个好的起点。https://www.discovermeteor.com/blog/meteor-methods-client-side-operations/在我的情况下,我插入的几乎每个文档都有一个日期戳&附加了userId,所以我必须使用方法,否则用户可以在客户端上声明任意Id。即使没有必要,这也只是一种更简单的思维方式,而不是在你的脑海中运行允许/拒绝逻辑(记住,1 TRUE,你就完蛋了)。这就是为什么有些人建议只使用deny,或者安装另一个软件包。。。

  2. 服务器上的Meteor使用光纤,这使得代码LOOK同步。https://www.eventedmind.com/feed/nodejs-using-futures我会通过将您的insert移动到Meteor.method来组织此操作。然后,我会用你从客户那里收集的文件打电话给mandrill;已在服务器上验证。(IIRC Mandrill需要一个APIkey,所以你可能不希望它挂在客户端代码中)

  3. 研究collection2和简单模式。它避免了大量混乱的服务器端检查、拾取和清理。学习曲线有点高&还有其他固体包装,但这是非官方的流星事实标准。