jwkim
October 28, 2017, 1:01am
1
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…
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.
client js
Simply subscribe published ‘task’ data
Template.body.onCreated(function bodyOnCreated() {
Meteor.subscribe('tasks');
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
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
jwkim
October 28, 2017, 9:09am
5
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.
jwkim
October 28, 2017, 9:15am
6
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:
jwkim
October 29, 2017, 1:57am
9
Wow that’s a so good new.Ok I will thanks a lot…good information
jwkim
October 30, 2017, 1:10am
10
Solved. Wow it was a silly happening.
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:
I believe it has to do with your subscription not being ready when that helper function is first being executed. Since your sub isn't ready your findOne() won't match any data as there isn't any. The reason why the name still shows up in the template and console, is that template helpers are basically Tracker.autorun and the findOne() creates a reactive dependency. So as soon as the data becomes available and your findOne() 'sees' the data, it will re-run. Try to wait for the sub to be ready …
=> return aUser && aUser.name ;
)
Summary
a mistake in helpers()
to make sure using this.subscribe(…); (when to use Template.subscriptionsReady)
1 Like