dennis
dennis

Reputation: 153

Mongo find() returns [object Object]

In meteor I've created a database array. The following is the contents:

meteor:PRIMARY> db.games.find()
{ "_id" : "ceg9JJ3u5abwqeyk7", "board" : [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ] }

In my client/main.js file inside my template helper I have:

Template.hello.helpers({
  counter() {
    return Template.instance().counter.get();
  },
  game() {
    return {
    game: Games.find()}
  }
});

In my main.html I have the following template:

<template name="hello">
  {{game}}
</template>

My output on the browser is:

 [object Object]

But I want to have the contents of my array (e.g. "0" ) put in the browser not "object".

How do I do this?

Upvotes: 1

Views: 8844

Answers (1)

Sudheer Jami
Sudheer Jami

Reputation: 737

You are not properly iterating over your query.

Template.hello.helpers({
  game() {
    return Games.find()
  }
});

main.html:

<template name="hello">
  {{#each game}}
    Boards: {{board}}
  {{/each}}
</template>

Explanation based on your comment:

There are 3 different ways to find records from your collection.

Collection.findOne(): Returns only 1 record as an object

Collection.find().fetch(): Returns all the records as an array of objects

Collection.find(): Returns a cursor (which is a function)

Please use your browser console to see the difference between each of these using below statements to get a better understanding:

console.log(Games.findOne());
console.log(Games.find().fetch());
console.log(Games.find());

All of these will return you your entire board field data as all the data is stored as a single record.

So you have to store this data in one of the following ways in-order for you to filter the data as per your requirement.

Method 1:

Store your data in the below format as individual record for each game:

{
    "_id" : "ceg9JJ3u5abwqeyk7",
    "name": "Game-1",
    "score": [ 0, 0, 0 ]
 },{
    "_id" : "bzv778zv6qge7xc8",
    "name": "Game-3",
    "score": [ 0, 0, 0 ]
 },{
    "_id" : "eji3ds9jo8yhs7739",
    "name": "Game-3",
    "score": [ 0, 0, 0 ]
 },

You can now display the data using below code:

<template name="hello">
  {{#each game}}
       Game Name: {{name}}, Score: {{score}} <br/>
  {{/each}}
</template>

If you want to display only the "Game-3" score, the you can filter while fetching the data using find:

<template name="hello">
  {{#each game}}
      Game Name: Game-3, Score: {{score}}
  {{/each}}
</template>

Template.hello.helpers({
  game() {
    return Games.find({name:"Game-3"});
  }
});

Method 2:

Store your data in the below format:

{
    "_id" : "ceg9JJ3u5abwqeyk7",
    "board" : [
                  {
                     "name": "Game-1",
                      "score": [ 0, 0, 0 ]
                  },
                  {
                     "name": "Game-2",
                      "score": [ 0, 0, 0 ]
                  },
                  {
                     "name": "Game-3",
                      "score": [ 0, 0, 0 ]
                  },
              ]
 }

You can display the data using below code:

<template name="hello">
  {{#each game}}
      Boards: <br/>
      {{#each board}}
         Game Name: {{name}}, Score: {{score}} <br/>
      {{/each}}
  {{/each}}
</template>

If you want to display only the "Game-3" score, the you can filter using a helper as shown below:

<template name="hello">
  {{#each game}}
      Boards: <br/>
      {{#each board}}
          {{#if isThirdGame}}
              Game Name: Game-3, Score: {{score}}
          {{/if}}
      {{/each}}
  {{/each}}
</template>

Template.hello.helpers({
  game() {
    return Games.find()
  },
  isThirdGame() {
    return this.name === "Game-3" ? true : false;
  },
});

Upvotes: 3

Related Questions