Router.url()在Email.send()中返回undefined

时间:2015-02-09 16:07:05

标签: meteor iron-router

我正在尝试为某些用户构建电子邮件。代码正在运行服务器端。在电子邮件中,我希望有一个链接供用户点击,但我没有太多运气。

我正在尝试使用Router.url()来设置锚点的href。我可以console.log()看到路由器对象至少被定义,但链接最终变得奇怪。

代码如下所示:

Meteor.methods({
  sendSubmissionEmail: function(responseId) {
    // Let other method calls from the same client start running,
    // without waiting for the email sending to complete.
    this.unblock();

    var formResponse = FormResponses.findOne({_id: responseId});
    var toEmails = [];
    _.each(Roles.getUsersInRole('ADMIN').fetch(), function(user) {
      if (user.profile && user.profile.receivesResponseEmails) {
        var email = _.findWhere(user.emails, {verified: true});
        if (!email) {
          console.log('No verified email address was found for ' + user.username + '. Using unverified email instead.');
          email = _.first(user.emails);
        }
        if (email) {
          toEmails.push(email.address);
        }
      }
    });

    if (toEmails && toEmails.length > 0) {
      console.log('Sending an email to the following Admins: ' + toEmails);
      console.log('Router: ', Router);
      Email.send({
        from: 'noreply@strataconsulting.us',
        to: toEmails,
        subject: 'Form Response for Form "' + formResponse.form_title + '" Ready For Approval',
        html: '<p>Form Response for Form <a href="' + Router.url('editResponse', formResponse.id) + '">' + formResponse.formTitle + '</a> is now ready for your approval.</p>'
      });
    }
  }
});

由此产生的电子邮件:

====== BEGIN MAIL #0 ======
MIME-Version: 1.0
From: noreply@strataconsulting.us
To: testuser4@codechimp.net
Subject: Form Response for Form "undefined" Ready For Approval
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<p>Form Response for Form <a href=3D"undefined">Test Form One</a> is now ready for your approval.</p>
====== END MAIL #0 ======

首先,在第一个“href”之前出现一个奇怪的“3D”,然后返回或者Router.url()总是undefined。为了确保通话是正确的,我模拟了在Chrome的开发工具控制台中执行以下操作:

var fr = FormResponses.findOne({_id: '1234567890'});
Router.url('editResponse', fr);

正如预期的那样,这会使用正确的ID集向我的editResponse路径吐出完整的URL路径。 Router.url()是仅限客户端的呼叫吗?如果是这样,我如何获得路由服务器端的URL?所有路由都是为客户端和服务器定义的。

1 个答案:

答案 0 :(得分:1)

下面:

var fr = FormResponses.findOne({_id: '1234567890'});
Router.url('editResponse', fr);

您正在将查找结果作为参数传递。它看起来像

{_id:...,otherStuff:...}

但是在你的代码中你没有传递一个对象,你只是传递一个字符串:

Router.url('editResponse', formResponse.id)

这解释了“未定义”。

3D很奇怪。