Meteor collection fetch returns empty array but is subscribed

后端 未结 1 1167
攒了一身酷
攒了一身酷 2020-12-17 08:11

I uninstalled the autosubscribe and restarted the meteor app. Since then, I haven\'t been able to access my collection data on the client.

Every question related to

相关标签:
1条回答
  • 2020-12-17 08:38

    I think you are getting [] because you are publishing the data on the startup, when isn't ready, lets make that subscribe reactive.

    Tracker.autorun(function(){
       Meteor.subscribe("states", function(){
          console.log(states, states.find(), states.find().fetch());
       });
    });
    

    OPTIONAL

    There is no reason to declare the collections inside the isServer/isClient if statements

    Since you are starting with the Good practices (removing insecure/autopublish packages)

    Lets do the follow.

    First Create the folder structure. (check meteor/structuringyourapp and this SO).

    Inside the appName/lib/collection.js put this code.

    states = new Meteor.Collection("states");
    //optional you can place this subscribe inside the appName/client/main.js
    if(Meteor.isClient){
       Meteor.subscribe("states", function(){
          console.log(states, states.find(), states.find().fetch());
       });
    }
    

    and on the appName/server/publish.js

    Meteor.publish("states", function () {
        return states.find();
      });
    
    0 讨论(0)
提交回复
热议问题