1
votes

I have created a very simple example to try get my head round publish and subscribe.

I removed:

autopublish insecure

My mongo database looks like this

meteor:PRIMARY> db.country.find() { "_id" : ObjectId("5332b2eca5af677cc2b1290d"), "country" : "new zealand", "city" : "auckland" }

My test.js file looks like this

var Country = new Meteor.Collection("country");

if(Meteor.isClient) {
    Meteor.subscribe("country");
    Template.test.country = function () {
        return Country.find();
      };
  }

if(Meteor.isServer) {
    Meteor.publish("country", function() {
        return Country.find();
    });
}

my html file looks like this

<head>
    <title>test</title>
</head>

<body>
    {{> test}}
</body>

<template name="test">
    <p>{{country}}</p>
</template>

I don't understand why this wouldn't work. I publish on the server, subscribe to it. I know this wouldn't be something I would do on a live environment but I can't even replicate retrieving the entire collection to view on the client.

If I change this return Country.find(); to return Country.find().count(); I get 1. The country text however does not appear.

Would love to know what is happening. I am new to development and using Meteor. I really like the framework.

Cheers

2
what do you get in client console if you type Country.findOne()? I think it may just be that the browser is unable to display the array of objects you returned by Template.test.country helper. - user728291
I get Country is not defined, thanks for reply - John Mellows

2 Answers

2
votes

Everything works as it should. If you want to print out all the documents, you must use the each helper:

<template name="test">
    {{#each country}}
        <p>{{country}}, {{city}}</p>
    {{/each}}
</template>
0
votes

Thank you Peppe L-G that worked, I modified my .js file slightly here is the end result:

.js file

var Country = new Meteor.Collection("country");

if(Meteor.isClient) {

    Meteor.subscribe("country");
    Template.test.countries = function () {
    return Country.find();
  };
}

if(Meteor.isServer) {
    Meteor.publish("country", function() {
        return Country.find();
    });
}

html file

<head>
  <title>test</title>
</head>

<body>
  {{> test}}
</body>

<template name="test">
    {{#each countries}}
        <p>{{country}}, {{city}}</p>
    {{/each}}
</template>

Since my code was almost correct, why is it I could not query the Console with Country.findOne() or see the collection by typing in Country? Making this data available on the Client I thought I would be able to still query from the console since I have not implemented any Methods.

Thank you though for the help.

Cheers