Search code examples
meteoriron-router

How to route with Iron Router to the submitted post?


I need to submit a post on the server side of Meteor application. The post is submitted okay, but router is not routing to 'problemPage' and it throws an error saying - Exception in delivering result of invoking 'insertProblem': Error: Missing required parameters on path "/problems/:_id". The missing params are: ["_id"]. The params object passed in was: {}. What am I doing wrong ?

'submit form':function(event) {
    event.preventDefault();

    var post = {
    postProblem: $(event.target).find('[name=problem]').val(),
    postWhy1: $(event.target).find('[name=why1]').val(),
    postWhy2: $(event.target).find('[name=why2]').val(),
    postWhy3: $(event.target).find('[name=why3]').val(),
    postWhy4: $(event.target).find('[name=why4]').val(),
    postWhy5: $(event.target).find('[name=why5]').val(),
    postSolution:$(event.target).find('[name=solution]').val(),
    submitdate: new Date()
    };

    Meteor.call('insertProblem', post, function(result) {
        Router.go('problemPage', {_id: result._id});
      });
  }
});

Problems = new Meteor.Collection("problems");

Meteor.methods({
	insertProblem: function(post) {
		var postId = Posts.insert(post);

		return {
			_id: postId
		};
	}
});

and the router of course :

// redirect to the current submitted problem
Router.route('problems/:_id', {
	name: 'problemPage',
	data: function() { return Problems.findOne(this.params._id);
	}
});


Solution

  • The router.go should work like this.

    Router.go('problemPage', result._id);
    

    but the error its complaining about getting an empty object, so lets do this, change the meteor.call to this.

    Meteor.call('insertProblem', post, function(error,result) {
            if(!error){
              console.log(result)
              console.log(result._id)
              Router.go('problemPage' + result._id); //it may work to with the + operator
            }
          });