I'm getting a document from MongoDB in Meteor like this:
Template.SubTasks.helpers ({
subelement: function (){
var sublen = TodosCol.find(this);
// var sublen2 = ????
return sublen2;
}
});
It works great, sublen returns as object. But when I'm, trying to convert it to array (for example like:
var sublen2 = sublen.toArray();
or
var sublen = TodosCol.find(this).toArray();
or the whole collection (without 'this'), it doesn't work and I get an "undefined is not a function" error in chrome console.
I also tried
var sublen2 = sublen.propertyName;
since it's an object, with no luck.
What am I doing wrong?
Thanks
EDIT:
I'm trying to iterate over values stored in array in a mongo document. I want to output them for example as separate div's. It's a simple task list. Tasks are iterating just great, but I'm trying to output subtasks assigned to a specific task. Subtasks are stored in the same document as their 'parent tasks' as an array of strings.
Template:
<template name='SubTasks'>
<div class='sub-output {{_id}}'>
{{#each subelement}}
<div class='sub-task {{_id}}'>
{{subtask}}
</div>
{{/each}}
</div>
</template>
If
TodosCol
is a collection, then its.find()
method returns a Cursor - http://docs.meteor.com/#/basic/Mongo-Collection-find. The cursor can be used to iterate through the results efficiently and reactively using{{each}}
.As the documentation explains, if you want the results of the
find()
call, you need to call.fetch()
- learn more at http://docs.meteor.com/#/full/mongo_cursor