Search code examples
meteoriron-router

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


I am trying to construct an email for some users. Code is running server-side. In the email I would like to have a link for the users to click on, but I am not having much luck.

I am trying to use Router.url() to set the href of an anchor. I can do console.log() and see the Router object is at least defined, but the link ends up being strange.

The code looks like this:

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>'
      });
    }
  }
});

And the resulting email:

====== 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 ======

First, there is a strange "3D" appearing prior to the first " of the href, then the return or Router.url() is always undefined. Just to make sure the call was right, I simulated it in Chrome's dev tools console by doing the following:

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

As expected this spits out the full URL path to my editResponse route with the correct ID set. Is Router.url() a client-only call? If so, how do I get the URL to a route server-side? All routes are defined for both client and server.


Solution

  • Here:

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

    You're passing the result of the Find as a parameter. It'll look like

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

    But in your code you're not passing an object, you're just passing a string:

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

    That explains the "undefined".

    The 3D is very odd.