Jesse James Richard
Jesse James Richard

Reputation: 493

Returning MongoDB Items in Meteor

I'm trying to understand how Meteor returns database records. I'm running the following code:

Template.body.helpers({
  items(){
    return Items.find({});
  },
  json(){
    console.log(this.items());
  },
  loggedIn(){
    return Meteor.userId();
  }
});

I'm a little confused as to why this json method doesn't just output and array, or more specifically why the child values don't really seem to just return an array of values.

I can get the values inline html using spacebars, but I'm not sure how to access those values through js. What simple thing am I missing here?

Upvotes: 0

Views: 46

Answers (2)

Michel Floyd
Michel Floyd

Reputation: 20256

Collection.find() in Meteor returns a cursor, which is a function that can be used by Blaze templates (for example).

Collection.find().fetch() returns an array of objects (i.e. documents).

Upvotes: 2

NRiebesel
NRiebesel

Reputation: 317

If you want to parse the database record between multiple helpers or even between templates and routes, why don't you use session variables.

For your example:

Template.body.helpers({
  items(){
    const items = Items.find({});
    Session.set('itemArray', items);
    return items;
  },
  json(){
    console.log(Session.get('itemArray');
  },
  loggedIn(){
    return Meteor.userId();
  }
});

Does this work for you?

Upvotes: 1

Related Questions