An exception throwing when I query db from Iron Router

Something strange here with Meteor 1.2.1 and Iron Router 1.0.12.

Router.route('/news/:_id', function() {
    this.render('l_basic');
    console.log (newsCollection.findOne().title);
});

This works perfect. I’ve got the title of my last post in the console.

But there is an unwanted exception too. No matter where I would place database query: into the main router function, to the onAfterAction or any other hook. Doesn’t matter, if I’ll surround it with if (this.ready()).

If I comment console.log statement, no exception appears.

This is what I get in console. I’ve completely broken my head, trying to find out what is going on here.

Exception in callback of async function: http://localhost:3000/app/both/router.js?4bb8a45e172aaff7cfe3c5a6bff0f87a62d217d0:17:59
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?3370bd57ef7b310cca3f5dddb11b77fafdcfc1eb:418:35
http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:27
onRerun@http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:515:13
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?3370bd57ef7b310cca3f5dddb11b77fafdcfc1eb:418:35
http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:27
onRun@http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:499:15
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?3370bd57ef7b310cca3f5dddb11b77fafdcfc1eb:418:35
http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:27
dispatch@http://localhost:3000/packages/iron_middleware-stack.js?3370bd57ef7b310cca3f5dddb11b77fafdcfc1eb:442:7
_runRoute@http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:538:17
dispatch@http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:848:27
route@http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:705:19
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?3370bd57ef7b310cca3f5dddb11b77fafdcfc1eb:418:35
http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:27
boundNext@http://localhost:3000/packages/iron_middleware-stack.js?3370bd57ef7b310cca3f5dddb11b77fafdcfc1eb:365:18
http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:27
dispatch@http://localhost:3000/packages/iron_middleware-stack.js?3370bd57ef7b310cca3f5dddb11b77fafdcfc1eb:442:7
http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:385:21
_compute@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:349:36
Computation@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:237:18
autorun@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:588:34
http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:383:17
nonreactive@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:615:13
dispatch@http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:382:19
dispatch@http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:1692:22
onLocationChange@http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:1776:33
_compute@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:349:36
Computation@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:237:18
autorun@http://localhost:3000/packages/tracker.js?7776276660c988c38fed448d8262b925dffb5bc3:588:34
start@http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:1769:43
http://localhost:3000/packages/iron_router.js?c564289eeaa191561eba900052037432ebfcbe4a:972:21
withValue@http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:971:21
http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:428:54
http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:27
onGlobalMessage@http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:365:23

My best advice is to route with the router, and handle data with the template. I haven’t looked back since I moved on to flow-router. ReactRouter is also awesome if you’re into React :slight_smile:

Thanks for advice. May be that would be the right decision. Is flow-router is some kind of main stream for Meteor now? I used iron:router in the couple of projects in past, and there was no problems. I’m still on Blaze, so ReactRouter is not an option.

I used router hooks to attach some data to title and meta tags. How can I do it within the template? I found that it is impossible to use helper-functions and include them with spacebars in head section of the document.

kadira:flow-router is what the opinionated guide from Meteor suggests, as it designed very concisely compared to iron:router, which is “juggling balls outside the routing park”. There’s a number of benefits to having templates deal with data/validation/whatever, and just let the router route. Here’s a nice article on the subject by Kadira.

for Blaze, I usually combine kadira:flow-router and kadira:blaze-layout, as they’re very concisely designed and very well documented. They really get the job done, once you’ve tried working with it a bit.

Here’s a few results you can look at from atmosphere, that deal with meta tags - some will probably allow you to work with the title aswell - take a look! :smile:

Thanks again. Sounds simple and good. I’ll definitely give flow-router a chance. And I think it’s possible to manage meta-data without router itself by using template hooks (I don’t like extra packages for such a simple functionality).

What about my initial question, it was such a stupid mistake: I just had to wait for subscriptions from iron:router itself. If this.ready() is not enough. It have to be something like this:

Router.route('/news/:_id', function() {
    this.render('l_basic');
    console.log (newsCollection.findOne().title);
}, {
  waitOn: function () {
    return Meteor.subscribe('news');
});

My initial code printed both the exception and the title to console just because of reactivity.