Search code examples
meteoriron-routerspacebars

Meteor Iron:Router Template not Rendering


I have a main page which lists a few text items ("Ideas"), which are clickable links. Clicking on them should take you to a page where you can edit them. Here's my html:

<head>
    <title>Ideas</title>
</head>

<body>
</body>

<template name="Ideas">
    <ul>
        {{#each ideas}}
            {{> idea}}
        {{/each}}
    </ul>
</template>

<template name="idea">
    <li><a href="/idea/{{_id}}">{{text}}</a></li>
</template>

<template name="ShowIdea">'
    <div class="editable" contentEditable="true">{{text}}</div>
</template>

I've added Iron:Router to my project to allow for moving between the pages. Here's the javascript:

Ideas = new Mongo.Collection("ideas");

if (Meteor.isClient) {
    Router.route('/', function() {
        this.render('Ideas');
    });

    Router.route('/idea/:_id', function() {
        var idea = Ideas.findOne({_id: this.params._id});
        this.render('ShowIdea', {text: idea.text});
    });

    Template.Ideas.helpers({
        ideas: function () {
            return Ideas.find({});
        }
    });
}

I inserted a single idea to my Mongo DB using the Meteor Mongo command line tool. That single item shows up properly on my main page. Here's what the HTML looks like in my debugger for the main page:

<html>
    <head>...</head>
    <body>
        <ul>
            <li>
                <a href="/idea/ObjectID("550b7da0a68cb03381840feb")">The first idea ever</a>
            </li>
        </ul>
    </body>
</html>

Clicking on that link takes me to a new page with an address of:

http://localhost:3000/idea/ObjectID(%22550b7da0a68cb03381840feb%22)

But nothing shows up on the page. In the debugger console I see this error message + stack trace, but it means nothing to me since it all seems to be pertaining to iron-router and meteor, not code which I actually wrote:

Exception in callback of async function: http://localhost:3000/Idea.js?2fd83048a1b04d74305beae2ff40f2ea7741d40d:10:44
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:424:35
http://localhost:3000/packages/meteor.js?e53378596562e8922a6369c955bab1e047fa866b:978:27
onRerun@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:520:13
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:424:35
http://localhost:3000/packages/meteor.js?e53378596562e8922a6369c955bab1e047fa866b:978:27
onRun@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:505:15
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:424:35
http://localhost:3000/packages/meteor.js?e53378596562e8922a6369c955bab1e047fa866b:978:27
dispatch@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:448:7
_runRoute@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:543:17
dispatch@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:844:27
route@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:710:19
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:424:35
http://localhost:3000/packages/meteor.js?e53378596562e8922a6369c955bab1e047fa866b:978:27
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:371:18
http://localhost:3000/packages/meteor.js?e53378596562e8922a6369c955bab1e047fa866b:978:27
dispatch@http://localhost:3000/packages/iron_middleware-stack.js?0e0f6983a838a6516556b08e62894f89720e2c44:448:7
http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:390:21
_compute@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:308:36
Computation@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:224:18
autorun@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:499:34
http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:388:17
nonreactive@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:525:13
dispatch@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:387:19
dispatch@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:1688:22
onLocationChange@http://localhost:3000/packages/iron_router.js?a427868585af16bb88b7c9996b2449aebb8dbf51:1772:33
_compute@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:308:36
_recompute@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:322:22
flush@http://localhost:3000/packages/tracker.js?21f0f4306879f57e10ad3a97efe9ea521c5b5775:452:24

And then it ends with this warning message:

Route dispatch never rendered. Did you forget to call this.next() in an onBeforeAction?

I don't have an onBeforeAction (I'm not even sure what that is)... so I don't think that message pertains to me?

I just started using Meteor the other day and just added iron-router not 24 hours ago, so I'm a bit lost here. Any pointers on how I can debug and fix this would be great.


Solution

  • Two things need fixing:

    1. When you insert documents from the shell they are assigned _id values which are mongo ObjectIDs, whereas meteor defaults to using strings. This explains the weird URL. To avoid this problem, it's generally best to initialize your data from the server. Here's an example:
    if (Meteor.isServer) {
      Meteor.startup(function() {
        if (Ideas.find().count() === 0) {
          Ideas.insert({text: 'feed the cat'});
        }
      });
    }
    

    Now after a $ meteor reset you will always start with one cat-related idea.

    1. If you wish to pass a context to your template, you'll need to use the data attribute like so:
    Router.route('/idea/:_id', function() {
      this.render('ShowIdea', {
        data: function () {return Ideas.findOne({_id: this.params._id})}
      });
    });
    

    See this example from the docs. After making those changes, the code worked correctly for me.