Note that there are some explanatory texts on larger screens.

plurals
  1. POcursor.find() callbacks not returning in meteorjs
    primarykey
    data
    text
    <p>I am returning results from a collection with the second result dependent on the first. So, I find resultA, and resultB has to be anything not equal to result A. </p> <p>My console.log statement shows the data that I want, however, when I return it, nothing shows up in my handlebars template. Nothing returns even if I JSON.stringify it. </p> <pre><code>myCollection.find({}, {limit:1, skip: _.random(minimum, maximum)}).forEach(function(a){ myCollection.find({'': {$ne: a._id}}, {limit:1}).forEach(function(b){ console.log({resultA: a, resultB: b}) return {resultA: a, resultB: b}; }); }); </code></pre> <p>What am I doing wrong here? </p> <p>Now I have tried using deps.autorun just incase the collections aren't ready (which they are because console.log always works)</p> <pre><code>Template.votes.helpers({ twoItems: function(){ var minimum = 0; Deps.autorun(function(){ var maximum = (Items.find().fetch().length) - 1; if (maximum){ var itemA = Items.find({}, {limit:1, skip: _.random(minimum, maximum)}).fetch()[0]; } if(itemA){ console.log(itemA); var itemB = Items.find({'': {$ne: itemA._id}}, {limit:1}).fetch()[0]; } if (itemB) { data = {itemA:itemA, itemB:itemB}; console.log(data); return data; } }); },}); </code></pre> <p>with a simple template:</p> <pre><code>&lt;template name="votes"&gt; &lt;p&gt;Test&lt;/p&gt; &lt;hr&gt; {{twoItems}} &lt;hr&gt;&lt;/template&gt; </code></pre> <p>At the very least it should return Object but nothing. Console.log always returns the correct data but nothing shows up in the rendered handlebars templates. </p> <p>Edit: I ended up having to use the following:</p> <pre><code> twoItems: function(){ var minimum = 0; var maximum = (Items.find().fetch().length) - 1; var itemA = Items.find({}, {limit:1, skip: _.random(minimum, maximum)}).forEach(function(a){ Session.set('itemAid', a._id); }); var itemA = Items.find({'_id': Session.get('itemAid')}).fetch()[0]; var itemB = Items.find({'_id': {$ne: Session.get('itemAid')}}, {limit:1,skip: _.random(minimum, maximum - 1)}).fetch()[0]; return {itemA: itemA, itemB: itemB}; }, </code></pre> <p>I still don't know why call backs aren't able to return anything. In the docs it says </p> <blockquote> <p>If you include a callback function as the last argument [In a Meteor.Call] (which can't be an argument to the method, since functions aren't serializable), the method will run asynchronously: it will return nothing in particular and will not throw an exception.</p> </blockquote> <p>So I'm assuming that since a collection operation is a meteor call as well, then that is why the returns are being ignored.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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