Meteor: How can I tell when the database is ready?

后端 未结 6 921
醉话见心
醉话见心 2020-12-01 11:39

I want to perform a Meteor collection query as soon as possible after page-load. The first thing I tried was something like this:

Games = new Meteor.Collecti         


        
相关标签:
6条回答
  • 2020-12-01 11:41

    Use DDP._allSubscriptionsReady() (Meteor 0.7)

    0 讨论(0)
  • 2020-12-01 11:43

    You should first publish the data from the server.

    if(Meteor.isServer) {
        Meteor.publish('default_db_data', function(){
            return Games.find({});
        });
    }
    

    On the client, perform the collection queries only after the data have been loaded from the server. This can be done by using a reactive session inside the subscribe calls.

    if (Meteor.isClient) {
      Meteor.startup(function() {
         Session.set('data_loaded', false); 
      }); 
    
      Meteor.subscribe('default_db_data', function(){
         //Set the reactive session as true to indicate that the data have been loaded
         Session.set('data_loaded', true); 
      });
    }
    

    Now when you perform collection queries, you can check if the data is loaded or not as:

    if(Session.get('data_loaded')){
         Games.find({});
    }
    

    Note: Remove autopublish package, it publishes all your data by default to the client and is poor practice.

    To remove it, execute $ meteor remove autopublish on every project from the root project directory.

    0 讨论(0)
  • 2020-12-01 11:46

    As of Meteor 1.0.4, there is a helper that tells you exactly when a particular subscription is ready: Template.instance().subscriptionsReady().

    Since this question is a duplicate, please check my answer in the original question, Displaying loader while meteor collection loads.

    0 讨论(0)
  • 2020-12-01 11:54

    Here is another tidbit of information for those who may be using userid or some part of user info stored in Meteor.users database. When the page first loads the Meteor subscribe, going on in the background, may not be complete itself. Therefor when you try to connect to another database to query for that, it will not pull the information. This is because the Meteor.user() is still null itself;

    The reason, like said above, is because the actual Meteor users collection has not gotten through pulling the data.

    Simple way to deal with this.

    Meteor.status().connected

    This will return true or false, to let you know when the Meteor.user collection is ready. Then you can go on about your business.

    I hope this helps someone, I was about to pull my hair out trying to figure out how to check the status. That was after figuring out the Meteor user collection itself was not loaded yet.

    0 讨论(0)
  • 2020-12-01 12:02

    You can also do template level subscriptions:

    Template.name.onCreated(function(){
    var self = this;
    
    this.autorun(function(){
    
    const db = this.subscribe('publicationname', [,args]);
    
    if(db.isReady()){
     "You'll know it's ready here" .. do what you need.
     }
     });
    })
    

    This makes it easier to know inside the template too. you can just call {{#if Template.subscriptionsReady}} {{else}} Loading Screen may be
    {{/if}}

    0 讨论(0)
  • 2020-12-01 12:04

    You could check when a result is finally returned if you know that your Games collection is never empty:

    Meteor.autorun(function() {
        if(Games.findOne() && !Session.get("loaded")) {
            Session.set("loaded",true);
    
            //Its ready..
            console.log(Games.findOne({}));
        }
    });
    

    You can also use this in your templates:

    Client js:

    Template.home.isReady = function() { return Session.get("loaded") };
    

    Html

    <template name="home">
        {{#if isReady}}
            Yay! We're loaded!!
        {{else}}
            Hold an a second
        {{/if}}
    </template>
    
    0 讨论(0)
提交回复
热议问题