Meteor Iron Router error : has no method 'go'


#1

After inserting a record into a collection in a server side method I route to a different named route. But I get an error : “has no method ‘go’”.

Meteor.methods({
  'create_item': function (item) {

    Items.insert(item, function (error,result){
      if(result){
        Router.go('dashboard');
      }
    });
  },
});

The route changes successfully and the page renders the dashboard template, but I get the following error.

I20160526-12:00:15.662(3)? Exception in callback of async function: TypeError: Object function router(req, res, next) {

I20160526-12:00:15.662(3)? router.dispatch(req.url, {

I20160526-12:00:15.662(3)? //XXX this assumes no other routers on the parent stack which we should probably fix

I20160526-12:00:15.662(3)? request: req,

I20160526-12:00:15.663(3)? }, next);

I20160526-12:00:15.662(3)? response: res

I20160526-12:00:15.663(3)? } has no method ‘go’

I20160526-12:00:15.663(3)? at lib/methods.js:17:16


#2

With Iron Router you can only call Router.go on the client side. Your Method is called on both the client and server, with the client side handling the Router.go call properly. The error is coming from the server side since the server doesn’t know how to handle Router.go. I’d suggest removing the Router.go part from your Method and instead handling this client side only when you make your Method call. Something like:

Meteor.call('create_item', item, (error, result) => {
  if (result) {
    Router.go('dashboard');
  }
});

Then your Method becomes something like:

Meteor.methods({
  create_item(item) {
    return Items.insert(item);
  },
});