【问题标题】:Meteor Router data function being called twiceMeteor Router 数据函数被调用两次
【发布时间】:2016-05-18 11:51:21
【问题描述】:

我有一个路由数据函数,它调用 Meteor 方法将新文档插入到集合中。我注意到文档被插入了两次,然后我注意到每次访问路由时都会调用数据函数本身两次。我不知道为什么会这样。

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")};

        }
    }
});

【问题讨论】:

    标签: javascript meteor iron-router


    【解决方案1】:

    我能够通过将 data 下的所有内容移动到 Router.onRun 挂钩来解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-03
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      • 2013-09-18
      • 2019-03-03
      相关资源
      最近更新 更多