L.T
L.T

Reputation: 2589

Collection can't get data in Template.mytemplate.rendered

I can't get data in Template.my.rendered ,but it is ok in Chrome's console. This is my code:

html:

<Template name="moitorContent">
  <div class="tab-pane active" id="1">
      ........
  </div>
</Template>

js:


    Template.moitorContent.rendered = function(){
         if(!this._rendered) {
          this._rendered = true;
          console.log(Svse.find({}).fetch());
        }
    }

When open the chrome with localhost:3000,

the console information is "[]"

but input them in chrome's console

 console.log(Svse.find({}).fetch()); 

console.log can get data

like this: enter image description here

Any idea about what I am doing wrong?

Any suggestions appreciated!

Upvotes: 1

Views: 169

Answers (1)

Tarang
Tarang

Reputation: 75955

This is happening because you're using console.log before the subscriptions are complete:

You should find that using this should work if this is the case:

Template.moitorContent.rendered = function(){
    console.log(Svse.find({}).fetch());
}

To get passed this you can check whether there is data in there first/or use a Session variable to check whether your subscription is complete.

Template.moitorContent.rendered = function(){
    if(!this._rendered && Svse.find({}).count()) {
      this._rendered = true;
      console.log(Svse.find({}).fetch());
    }
}

A cleaner way would be to remove the autopublish package & make a manual subscription & subscribe. Use a Session to mark subscriptions as complete & check whether the subscription is complete in your rendered & use that to do your next task with all the data available.

Upvotes: 1

Related Questions