Andy59469
Andy59469

Reputation: 2064

Meteor Iron Router error : has no method 'go'

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

Upvotes: 1

Views: 85

Answers (2)

Andy59469
Andy59469

Reputation: 2064

Thank you Ramil.

In the end I discovered it was the lib could not be found on server side. I also discovered the AutoForm hooks - which is a much smarter way to run the post insert code.

I attached this hook to the Iron Route (well the Iron Route Controller to be exact)

onRun: function () {
    AutoForm.hooks({
      createItemForm: {
        onSuccess: function(){
         Router.go('dashboard');
        }
      }
    });
    this.next();
  },

Upvotes: 1

Ramil Muratov
Ramil Muratov

Reputation: 546

You probably defined method on shared area (e.g. lib directory), so on the client it works correct, but on the server side there is no such function as Router.go.

You should return result from method and then call Router.go on client side code.

On the server:

Meteor.methods({
    'create_item': function (item) {
        //Insert blocks on server side,
        //no need to use callback
        return Items.insert(item);
    },
});

On client side:

Meteor.call('create_item', item, function(err, res) {
    if (err) {
        console.error(err);
    } else {
        Router.go('dashboard');
    }
});

Upvotes: 1

Related Questions