javascript - cursor.find() callbacks not returning in meteorjs -
i returning results collection second result dependent on first. so, find resulta, , resultb has not equal result a.
my console.log statement shows data want, however, when return it, nothing shows in handlebars template. nothing returns if json.stringify it.
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}; }); });
what doing wrong here?
now have tried using deps.autorun incase collections aren't ready (which because console.log works)
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; } }); },});
with simple template:
<template name="votes"> <p>test</p> <hr> {{twoitems}} <hr></template>
at least should return object nothing. console.log returns correct data nothing shows in rendered handlebars templates.
edit: ended having use following:
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}; },
i still don't know why call backs aren't able return anything. in docs says
if include callback function last argument [in meteor.call] (which can't argument method, since functions aren't serializable), method run asynchronously: return nothing in particular , not throw exception.
so i'm assuming since collection operation meteor call well, why returns being ignored.
i can duplicate problem wrapping working template helpers have in deps.autorun , - console log shows ran nothing in browser.
a more standard way write might be:
deps.autorun(function(){ var itema, itemb; var minimum = 0; var maximum = (items.find().fetch().length) - 1; if (maximum){ itema = items.findone({}, {skip: _.random(minimum, maximum)}); } if(itema){ console.log(itema); itemb = items.findone({ _id: {$ne: itema._id}}); } if (itemb) { var data = {itema:itema, itemb:itemb}; console.log(data); session.set( "twoitems", data); } }); template.votes.helpers({ twoitems: function(){ return session.get( "twoitems" ); } });
i expect show "[object, object]" in browser when run think expecting , change template call once being returned.
also in template last <hr>
should </hr>
.
Comments
Post a Comment