Search code examples
javascriptmeteoriron-router

Meteor Router data function being called twice


I have a router data function that calls a Meteor method to insert a new document into a collection. I noticed that the document was being inserted twice and then I noticed that the data function itself is called twice every time the route is visited. I can't figure out why this is happening.

Router.route('/myurl',{
    name: 'myurl',
    path: '/myurl',
    data: function () {
        console.log('dupe?');
      // the data function is an example where this.params is available

      // we can access params using this.params
      // see the below paths that would match this route
      var params = this.params;

      // we can access query string params using this.params.query
      var post = this.params.query;

      // query params are added to the 'query' object on this.params.
      // given a browser path of: '/?task_name=abcd1234
      // this.params.query.task_name => 'abcd1234'
      if(this.ready()){
          Meteor.call('points.add', post, function(error, result){
            if(error)
            {
                Session.set("postResponse", "failed");
            }
            else
            {
                Session.set("postResponse", "success");
            }
          });

          return {_message: Session.get("postResponse")};

        }
    }
});

Solution

  • I was able to fix this by moving everything under data to a Router.onRun hook.