Router.url() 在 Email.send() 中返回未定义

Router.url() returning undefined in Email.send()

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

我正在尝试使用 Router.url() 设置锚点的 href。我可以做 console.log() 并看到 Router 对象至少已定义,但 link 最终变得很奇怪。

代码如下所示:

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",然后 return 或 Router.url() 总是 undefined。只是为了确保调用正确,我通过执行以下操作在 Chrome 的开发工具控制台中模拟了它:

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

正如预期的那样,这会吐出完整的 URL 路径到我的 editResponse 路由,并设置了正确的 ID。 Router.url() 是仅客户端调用吗?如果是这样,如何将 URL 获取到路由服务器端?为客户端和服务器定义了所有路由。

这里:

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

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

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

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

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

这解释了 "undefined"。

3D很奇怪