Meteor: Tracker.autorun / observeChanges & collections not working as expected

后端 未结 1 1566
醉梦人生
醉梦人生 2021-01-14 06:11

I am new to using meteor, so I am hoping to receive a very basic explanation of how these functions work, and how I am supposed to be using them. Otherwise, if there is a me

相关标签:
1条回答
  • 2021-01-14 07:08

    that means that I am trying to access this document before the collection has loaded

    Seems like you get the problem, now lets get ride to some possible solutions.

    Meteor version 1.1

    If you are using the new meteor version 1.1 (you can check running meteor --version)

    use this.

    First on the onCreated function use this.

    Template.progressBar.onCreated(function () {
      var self = this;
    
      self.autorun(function () {
        self.subscribe("Progress");
      });
    });
    

    See more about subscriptionReady on the DOCS. Now on the HTML use like this.

    <template name="progress">
      {{#if Template.subscriptionsReady}}
          <div id="progress-bar" style="width:{{curValue}}; background-color:*dynamicColor*;"></div>
        {{else}}
           {{> spinner}} <!-- or whatever you have to put on the loading -->
       {{/if}}
    </template>
    

    Meteor under 1.0.4

    You can have on the router something like a waitOn:function(){}

    waitOn:function(){
      Meteor.subscribe("Progress");
    }
    

    or since helper are asynchronous do something like this (not recommendable).

    Template.progressBar.helpers({
      curValue: function () {
        query = Progress.findOne({user: Meteor.userId()}).curValue;
        if(query != undefined){
          return query;
        }else{
         console.log("collection isn't ready")
        }
      }
    });
    
    0 讨论(0)
提交回复
热议问题