Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 等待流星收集在下一步之前完成_Javascript_Meteor - Fatal编程技术网

Javascript 等待流星收集在下一步之前完成

Javascript 等待流星收集在下一步之前完成,javascript,meteor,Javascript,Meteor,我有一个流星模板,应该显示一些数据 Template.svg_template.rendered = function () { dataset_collection = Pushups.find({},{fields: { date:1, data:1 }}, {sort: {date: -1}}).fetch(); a = moment(dataset_collection[0].date, "YYYY/M/D"); //more code follows that is al

我有一个流星模板,应该显示一些数据

Template.svg_template.rendered = function () {
  dataset_collection = Pushups.find({},{fields: { date:1, data:1 }}, {sort: {date: -1}}).fetch();

  a = moment(dataset_collection[0].date, "YYYY/M/D");
  //more code follows that is also dependent on the collection being completely loaded
};
有时它有效,有时我会出现以下错误:

Deps afterFlush函数异常:TypeError:无法读取未定义的属性“date”

我没有在任何情况下使用Deps。据我所知,集合在加载完成之前就已经被引用了


因此,我想弄清楚如何简单地说“在继续之前等待集合被找到。”应该很简单,但找不到更新的解决方案

您是对的,您应该确保在正确加载数据后执行依赖于获取客户端订阅集合内容的代码

您可以使用Meteor 1.0.4中引入的新模式实现这一点:

client/views/svg/svg.js

Template.outer.onCreated(function(){
  // subscribe to the publication responsible for sending the Pushups
  // documents down to the client
  this.subscribe("pushupsPub");
});
Router.route("/svg", {
  name: "svg",
  template: "svgTemplate",
  waitOn: function(){
    // waitOn makes sure that this publication is ready before rendering your template
    return Meteor.subscribe("publication");
  },
  data: function(){
    // this will be used as the current data context in your template
    return Pushups.find(/*...*/);
  }
});
client/views/svg/svg.html

<template name="outer">
  {{#if Template.subscriptionsReady}}
    {{> svgTemplate}}
  {{else}}
    Loading...
  {{/if}}
</template>
或者,您可以使用
iron:router
(),它提供了另一种设计模式来实现这一常见的流星相关问题,即在路由级别而不是模板级别移动订阅处理

将包添加到项目中:

meteor add iron:router
lib/router.js

Template.outer.onCreated(function(){
  // subscribe to the publication responsible for sending the Pushups
  // documents down to the client
  this.subscribe("pushupsPub");
});
Router.route("/svg", {
  name: "svg",
  template: "svgTemplate",
  waitOn: function(){
    // waitOn makes sure that this publication is ready before rendering your template
    return Meteor.subscribe("publication");
  },
  data: function(){
    // this will be used as the current data context in your template
    return Pushups.find(/*...*/);
  }
});
使用这段简单的代码,您将获得所需的内容以及许多附加功能。 你可以看一看铁路由器指南,它详细地解释了这些特性


2015年3月18日编辑:修改了答案,因为它包含过时的材料,但仍然收到了更新票。

这是我希望meteor基本文档直接解决的问题之一。这令人困惑,因为:

  • 根据API,您做了正确的事情
  • 您会得到
    Deps
    的错误,但这并没有将您指向根本问题
  • 因此,正如您已经了解到的,当呈现模板时,您的数据还没有准备好。最简单的解决方案是什么?假设数据可能没有准备好。他们做了很多这方面的工作。发件人:

    只有在实际找到
    player
    时,才会访问
    player.name
    。在coffeescript中,您可以使用浸泡来完成相同的任务

    saimeunt建议的“s
    waitOn
    适用于此特定用例,但请注意,您很可能会在应用程序中遇到这样的情况:数据库中不存在数据,或者获取的对象上不存在您想要的属性


    不幸的是,在许多情况下,需要进行一些防御性编程。

    使用iron router等待订阅工作,但我喜欢将订阅集中管理在类似collections.js的文件中。取而代之的是,我利用Meteor的优势,在其他一切之前加载订阅

    下面是my collections.js文件的外观:

    // ****************************** Collections **********************************
    Groups = new Mongo.Collection("groups");
    
    // ****************************** Methods **************************************
    myGroups = function (userId) {
      return Groups.find({"members":{$elemMatch:{"user_id":userId}}});
    };
    
    // ****************************** Subscriptions ********************************
    if(Meteor.isClient){
      Meteor.subscribe("groups");
    }
    
    // ****************************** Publications *********************************
    if(Meteor.isServer){
      Meteor.publish("groups", function () {
        return myGroups(this.userId);
      });
    }
    
    然后,我将collections.js放入
    lib/
    文件夹,以便在加载典型的客户端代码之前加载它。这样,订阅将集中到单个collections.js文件中,而不是作为我的路由的一部分。此示例还集中了我的查询,因此客户端代码可以使用相同的方法提取数据:

    var groups = myGroups(Meteor.userId());
    

    谢谢你修改了你的答案。Meteor移动很快,有很多过时的信息。再次修改答案,以鼓励使用模板级订阅,这是一种设计模式,已进入Meteor的最新版本。应该是公认的答案。多亏了iron:router和你的回答,我才能够轻松地解决我的问题。谢谢你,伙计!:-)