Mongo document to array

919 views Asked by At

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>
3

There are 3 answers

0
Dan Dascalescu On BEST ANSWER

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

0
Rajanand02 On

I am assuming that you have a MongoDB Collection and inside that collection you have a key called subtasks which has an array.

Template.tasks.helpers({
  tasks:function(){
    return Tasks.find({}).fetch();
  }
});

Then in your HTML template.

<template name="tasks">
  {{#each tasks}}
    <div id ="{{_id}}">
        <p>{{title}}</p>
        <ul>
            {{#each subtasks}}
                <li>
                    <p>{{this}}</p> 
                </li>
            {{/each}}
        </ul>
    </div>
  {{/each}}
</template>
1
Ralph On

The problem is that javascript objects do not have a toArray() method. I can't offer much help without more specifics. Namely, what exactly do you want the array to contain? Objects contain key value pairs, also known as properties. You should log the object to the console when you first receive it and see what parts of the object you want to comprise your array.

If, for example, you want an array of keys/properties on the object, you might try sublen.keys() which returns an array of the string names of each property (keys) on the object.

Perhaps you want an array that is already stored in the object as the value to some key. By logging the object, you can find this key/property and access it via dot notation: sublen.<property-name>

go here to see what sorts of methods the js Object has on it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object