[Solved] Meteor.subscribe not receiving data

I googled much on this problem, including posts asking for help, and the official docs(This doesn’t seem like a trivial issue. For now I didn’t read them carefully, just skimmed through):

I wonder how official Todo example worked with such a simple code.


First, removed autopublish, and…

  1. html
    The problem is as the code below.

<body> ..

{{#if Template.subscriptionsReady}} 
  ok..showing
  {{#each tasks}}
  not showing
  {{/each}}

=>
under subscriptionsReady: ok…showing
under {{#each tasks}} : not get tasks. this is the problem.


  1. client js
  • Simply subscribe published ‘task’ data
Template.body.onCreated(function bodyOnCreated() {
    
  Meteor.subscribe('tasks');
  • and I also tried like

if (this.subscriptionsReady()) {
 Meteor.subscribe('tasks');

or

var subscription = Meteor.subscribe('tasks'); 
  if (subscription.ready()) {
    console.log("> Received posts. \n\n");      // printed

… and so on


  1. server side js call

This works well, called once when server started

if (Meteor.isServer) 
{
  Meteor.publish('tasks', function tasksPublication() {   
	// console .. ok list data printed  
    console.log("[tasks:", Tasks.find({}, { sort: { createdAt: -1 } }).fetch());
    return Tasks.find({}, { sort: { createdAt: -1 } });  
  });
}

Do you have a helper for tasks?

You need something like:

Template.body.helpers({
  tasks() {
    return Tasks.find({});
  },
});```
2 Likes

To use Template.subscriptionsReady you have to bind subscription to Template’s context using this.subscribe('tasks'); instead of Meteor.subscribe('tasks');

Template.body.onCreated(function bodyOnCreated() {
  this.subscribe('tasks');
});
2 Likes

I guess I’ve tried this.subscribe too…(according to the official doc)but not sure… I’ll recheck that. Thanks for making sure the problem point.

Wow good to see help. Thanks.
I wondered helpers()…about why it might be related to P/S because publish() returns data by itself…anyway so I once tried to remove it, but soon did undo…l’ll recheck that again.

Have a read of this as well and you’ll understand why you need to specify the data you want in three places:

Wow that’s a so good new.Ok I will thanks a lot…good information

Solved. Wow it was a silly happening.

  1. Main reason
    Going back to the very basic, I found everything was just ok:
    (Template.body.helpers({
    tasks: [ { text: ‘This is task 1’ }, … )

Problem was misusing “&& []” in helpers():
return Tasks.find({}, { sort: { createdAt: -1 } }) && [];

The intention was to avoid null return(|| []), but I did silly mistake.

( This was because in a rash moment, I misunderstood this:


=> return aUser && aUser.name;
)

  1. Summary
  1. a mistake in helpers()
  2. to make sure using this.subscribe(…); (when to use Template.subscriptionsReady)
1 Like