Kazi
Kazi

Reputation: 1553

Meteor subscribe is not loading data from the server

I am having difficulties with meteor 1.6. First, I have created a database instance and tried to subscribe in the client. through form, I am able to input the data into my database. But could not retrieve it through the subscribe. can anybody tell me what wrong have I done in my code?

import { Template } from "meteor/templating";
import { Notes } from "../lib/collection";
import { Meteor } from "meteor/meteor";
// import { ReactiveDict } from 'meteor/reactive-dict';
import "./main.html";
/* 
Template.body.onCreated(function bodyOnCreated() {
  this.state = new ReactiveDict();

  Meteor.subscribe("db1");
}); */

Template.Display.helpers({

  notes() {
    Meteor.subscribe("db1");
    return Meteor.call('data');
  }
});

Template.body.events({
  "click .delete": function() {
    Notes.remove(this._id);
  },

  "submit .formSubmit": function(event) {
    event.preventDefault();
    let target = event.target;
    let name = target.name.value;

    Meteor.call("inputs", name);

    target.name.value = "";

    return false;
  },
  "click .userDetail": function() {
    if (confirm("Delete the user Detail ?")) {
      Notes.remove(this._id);
    }
  }
});

here is the code for publication :

import { Mongo } from 'meteor/mongo';
export const Notes = new Mongo.Collection('notes');

Meteor.methods({
    inputs:(name)=> {
      if (!Meteor.user()) {
        throw Meteor.Error("Logged in");
      }
      Notes.insert({
        name: name,
        createdAt: new Date()
      });
    },
    data:()=>{
        return Notes.find({});
    }
  });

Upvotes: 0

Views: 88

Answers (1)

Ankur Soni
Ankur Soni

Reputation: 6018

Meteor.subscribe("notes"); should be in Template.body.onCreated lifycycle method. you need to write a publish code seperately and not inside the Meteor.method. see below format,

Meteor.publish('notes', function tasksPublication() {
    return Notes.find({});
});

Inside the helper just call the subscribed Collection a below,

Template.Display.helpers({
  notes() {
    return Notes.find({});
  }
});

**NOTE: ** Never use Meteor.call inside the helper method. helpers are reactive and real time.

Upvotes: 2

Related Questions