javascript - Meteor Router 数据函数被调用两次

标签 javascript meteor iron-router

我有一个路由器数据函数,它调用 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")};

        }
    }
});

最佳答案

我可以通过将 data 下的所有内容移动到 Router.onRun Hook 来解决此问题。

关于javascript - Meteor Router 数据函数被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37289730/

相关文章:

循环内的 JavaScript 闭包——简单实用的例子

javascript - 如何在 CodeMirror 编辑器中使用 selenium 模拟关键事件

Meteor:最好的调试工具?

node.js - 在 Meteor.js 上定义证书颁发机构

meteor - 错误 : No uiManager configured on Router

javascript - 有没有办法扩展 JavaScript 对象数组?

javascript - 当 URL 带有 www 时,XMLHttpRequest 无法加载

javascript - ubuntu 上的 meteor 错误。退出并显示代码 : 1 => Your application is crashing. 等待文件更改

nginx - Meteor 部署到子文件夹并出现 nginx 错误

meteor - Meteor.js 应用程序模板中的多产量