Meteor: collection empty after insert -
i new meteor , got stucked , can't understand doing wrong. enlighten me please.
here html file:
<body> <h1>do</h1> {{#if activetask}} {{> currenttask}} {{else}} {{> newtask }} {{/if}} <div> </div> </body> <template name="newtask"> <form> <label>what<input type="text" name="what" placeholder="gimme action"/></label> <input type="submit" value="go"/> </form> <!-- {{> inputautocomplete settings=settings id="msg" class="input-xlarge" placeholder="action"}} --> </template> <template name="currenttask"> <form> <label>what<input type="text" name="what" placeholder="gimme action"/>{{activetask.what}}</label> <div>4h 15m</div> <input type="submit" value="stop"/> </form> </template>
and here javascript file:
tasks = new mongo.collection('tasks'); if (meteor.isclient) { template.body.helpers({ activetask: function() { var task = tasks.findone( { endat: null }, { sort: { startat: -1 } } ); console.log(task); return task; } }); template.newtask.events({ 'submit' : function(event) { event.preventdefault(); var = date.now(); var = event.target.what.value; tasks.insert({ what: what, startat: now, endat: null }); } }); }
it adds new document database , logs in helper activetask. 1 step later logs no task @ all. has gone. why?
if don't have package autopublish
(https://atmospherejs.com/meteor/autopublish) installed, need create publication (client-side) , subscription (server-side):
if (meteor.isserver) { meteor.publish('tasks', function () { return tasks.find(); }); } if (meteor.isclient) { meteor.subscribe('tasks'); }
i've explained working collections in recent blog article.
Comments
Post a Comment