0
votes

I'm trying to build a very simple Meteor app. I have implemented all CRUD. But my problem is when I have use publish and then subscribe, no data is returned. My code is given below:

imports\api\tasks\tasks.js

import { Mongo } from 'meteor/mongo';
import { Meteor } from 'meteor/meteor';

TasksSchema = new SimpleSchema({
   title: {
    type: String
   },
   owner: {
     type: String
   }
 });

export const Tasks = new Mongo.Collection('tasks', { schema: TasksSchema });

if (Meteor.isServer) {
   Meteor.publish('task.list', function() {
       return Tasks.find({ owner: this.userId });
   });
}

imports\ui\components\home\home.js

import { Tasks } from '/imports/api/tasks/tasks.js';
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating'
import { ReactiveDict } from 'meteor/reactive-dict';

import './home.html';

if (Meteor.isClient) {
    Template.home.onCreated(function bodyOnCreated() {
       Meteor.subscribe('task.list');
    });

    Template.home.helpers({
        tasks() {
            let userId = Meteor.userId();
            return Tasks.find({ owner: userId }, { sort: { updatedAt: -1 } });
        }
   });
}

imports\ui\components\home\home.html

 <!-- here nothing is shown,although I have data -->
 {{#each task in tasks  }}

      {{task.title}} 

  {{/each}}

Any suggestion? Thanks in advance.

2
Instead of {{task.title}} try {{title}} and also check in browser console Task.find({}).fetch() and see whether you can see records or not - Sasikanth
It works @Sasikanth, Thank you - sabbir
I posted it as an answer you can accept it to close the question. - Sasikanth

2 Answers

0
votes

Instead of {{task.title}} try {{title}} and also check in browser console Task.find({}).fetch() and see whether you can see records or not.

0
votes

Its probably because your subscription isn't ready to be used on client. What you have to do is wrap your each code in subscriptionsReady function like below:

{{#if Template.subscriptionsReady}}    
  {{#each task in tasks  }}
    {{task.title}} 
  {{/each}}
{{else}}
  <p>Loading...</p>
{{/if}}

Above should work. You can check this link for more information of how to use subscriptionsReady