Note that there are some explanatory texts on larger screens.

plurals
  1. POMeteor.js: Client doesn't subscribe to collection
    primarykey
    data
    text
    <p>I created a little example for myself to test some stuff with Meteor. But right now it looks like I can't subscribe to a collection, I published on the server side. I hope somebody can tell me where the bug is.</p> <p><strong>server/model.js</strong></p> <pre><code>Test = new Meteor.Collection("test"); if (Test.find().count() &lt; 1) { Test.insert({id: 1, name: "test1"}); Test.insert({id: 2, name: "test2"}); } Meteor.publish('test', function () { return Test.find(); }); </code></pre> <p><strong>client/test.js</strong></p> <pre><code>Meteor.subscribe("test"); Test = new Meteor.Collection("test"); Template.hello.test = function () { console.log(Test.find().count());//returns 0 return Test.findOne(); } Template.hello.events = { 'click input' : function () { // template data, if any, is available in 'this' if (typeof console !== 'undefined') console.log("You pressed the button"); } }; </code></pre> <p><strong>client/test.html</strong></p> <pre><code>&lt;head&gt; &lt;title&gt;test&lt;/title&gt; &lt;/head&gt; &lt;body&gt; {{&gt; hello}} &lt;/body&gt; &lt;template name="hello"&gt; &lt;h1&gt;Hello World!&lt;/h1&gt; {{#with test}} ID: {{id}} Name: {{name}} {{/with}} &lt;input type="button" value="Click" /&gt; &lt;/template&gt; </code></pre> <p><strong>EDIT 1</strong></p> <p>I want to change the object test, findOne() returns. Let's say for adding an attribute avg which contains the average value of two numbers (test.number1 and test.number2). In my opinion this should look like the following code. But javascript is not synchronous, so this won't work.</p> <pre><code>Template.hello.test = function () { var test = Test.findOne(); test.avg = (test.number1 + test.number2) / 2; return test; } </code></pre> <p><strong>EDIT 2</strong></p> <p>This code worked for me. Now I have to rethink why this solution with 'if (test)' just works with findOne() without a selector in my original project.</p> <pre><code>Template.hello.test = function () { var avg = 0, total = 0, cursor = Test.find(), count = cursor.count(); cursor.forEach(function(e) { total += e.number; }); avg = total / count; var test = Test.findOne({id: 1}); if (test) { test.avg = avg; } return test; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload