programmingmusic
programmingmusic

Reputation: 489

Using the MeteorJS stack, how do I return properties of an Object using the id of that object?

I am very new(2 days) to MeteorJS so I apologize if this is basic. Here is my collection and insert statement:

Posts = new Mongo.Collection("posts");

...Posts.insert({
        title: title,
        body: body,
        createdAt: new Date(),
        userId: Meteor.userId()
    });
...

I am trying to get the title, and eventually the rest of the details of a blog post with a specific id(that a user has clicked on to view):

console.log(Posts.find({_id: this._id}).fetch('title'));

Here is what is currently being output:

 [Object]0: Object_id: "a6sxfuj2fzhjrLHwb"body: "Get a life Lebrec!"createdAt: Wed Apr 13 2016 22:14:17 GMT-0400 (Eastern Daylight Time)title: "Lebrec makes a fifth blog post."userId: "ST4g5DHWFL3wXZLdx"__proto__: Objectlength: 1__proto__: Array[0]

I am getting a whole object, along with other stuff. I am just looking for the title, "Lebrec makes a fifth blog post.", to be output to the console in this instance. The end goal is to display the title, date, email of user, and body of post in a modal. The modal is already functioning. Thanks in advance!

Upvotes: 0

Views: 25

Answers (1)

David Weldon
David Weldon

Reputation: 64312

Start with the "Find and Fetch" section of common mistakes. Here's what you are looking for:

console.log(Posts.findOne({_id: this._id}).title);

Upvotes: 2

Related Questions