How to access elements inside a collection?

I have a collection which is defined as follows:

Posts.insert({
                name: Meteor.user().profile.name,
                id: Meteor.userId,
                email: Meteor.user().services.google.email,
                image: Meteor.user().services.google.picture,
                title: titleVar,
                body: bodyVar,
                description: describeit,
                date: new Date(),
           


            });

Now, at some other point, I want to know the title of the post, by the loggedI user.

I have used Posts.find({id:Meteor.userId()}).fetch().title, but it returns undefined. Is there any other way?

When inserting the post, you need:

id: Meteor.userId(),

not

id: Meteor.userId,

(The missing parentheses mean the function isn’t called so the id value in the db will be undefined.)

Fetch returns an array, so you would have to say Posts.find({id:Meteor.userId()}).fetch()[0].title, which would fail if your find didn’t return anything. Also, it would only yield the title of the first post.

Another way would be Posts.findOne({id:Meteor.userId()}).title which would also fail if the user didn’t have a post.

I guess a complete solution (at least for rendering a list of posts by a single user) would be:

HTML

<template name="userPostsList">
  <ul>
    {{#each postsByCurrentUser}}
      <li>{{title}}</li>
    {{/each}}
  </ul>
</template>

JS

Template.userPostsList.helpers({
  postsByCurrentUser : function () {
    return Posts.find({id: Meteor.userId()});
  }
});

For just getting an array of titles:

var userPostTitles = Posts.find({id: Meteor.userId()}).map(function (post) {
  return post.title;
});