Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Ember.js stage 4 — individual post content not showing

Hey,

I'm really enjoying the Ember.js course, but I've got to the end of the video "Loading individual javascript objects" and my posts aren't displaying the content.

There's no error in the console, which is frustrating!

15 Answers

Michael Kaiser-Nyman
STAFF
Michael Kaiser-Nyman
Treehouse Guest Teacher

Got it. params.post_id returns something like "1". Notice that this is a string, not a number. In your store.js, your data looks like:

var posts = [
  {
    id: 1,
...

Notice that the 1 is a number, not a string.

So when you call posts.findBy('id', params.post_id), there is no object whose id property matches "1". If you change all the id properties to strings, you'll be on your way.

Michael Kaiser-Nyman
STAFF
Michael Kaiser-Nyman
Treehouse Guest Teacher

Hey Gary. Can you provide some of the code you're working with, and I can see if I can help you out?

Hey Michael. Sure:

router.js

Blogger.Router.map(function() {
  this.resource('posts', {path: '/'});
  this.resource('post',  {path: 'posts/:post_id'});
  this.resource('about');
  this.resource('contact', function(){
    this.resource('phone');
    this.resource('email');
  });
  this.resource('recent-comments');
});

post.js

Blogger.PostRoute = Ember.Route.extend({
  model: function(params) {
    return posts.findBy('id', params.post_id);
  }
});

application.hbs

<nav class="navbar navbar-default" role="navigation">
    <ul class="nav navbar-nav">
      <li>{{#link-to 'posts'}}Posts{{/link-to}}</li>
      <li>{{#link-to 'recent-comments'}}Recent Comments{{/link-to}}</li>
      <li>{{#link-to 'about'}}About{{/link-to}}</li>
      <li>{{#link-to 'contact'}}Contact{{/link-to}}</li>
    </ul>
  </nav>

post.hbs

<h1>{{title}}</h1>
<p>{{body}}</p>

store.js

var posts = [
  {
    id: 1,
    title: "I love blood",
    body: "Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam porta sem malesuada magna mollis euismod. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Cras mattis consectetur purus sit amet fermentum. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Aenean lacinia bibendum nulla sed consectetur. Morbi leo risus, porta ac consectetur ac, vestibulum at eros."
  },
  {
    id: 2,
    title: "Curabitur blandit tempus porttitor",
    body: "Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam porta sem malesuada magna mollis euismod. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Cras mattis consectetur purus sit amet fermentum. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Aenean lacinia bibendum nulla sed consectetur. Morbi leo risus, porta ac consectetur ac, vestibulum at eros."
  },
  {
    id: 3,
    title: "Justo Fusce Inceptos",
    body: "Donec sed odio dui. Nullam quis risus eget urna mollis ornare vel eu leo. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam porta sem malesuada magna mollis euismod. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Cras mattis consectetur purus sit amet fermentum. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Aenean lacinia bibendum nulla sed consectetur. Morbi leo risus, porta ac consectetur ac, vestibulum at eros."
  }
];



var comments = [
  {
    id: 1,
    postId: 3,
    text: "Donec sed odio dui. Nullam quis risus eget urna mollis"
  },
  {
    id: 2,
    postId: 3,
    text: "Mollis Justo Malesuada Mattis Ultricies. Cum sociis natoque penatibus"
  },
  {
    id: 3,
    postId: 1,
    text: "Nullam Elit Nibh Magna."
  }
];

Might also be worth pointing out that i'm using Ember 1.8.1, in case that makes a difference.

Michael Kaiser-Nyman
STAFF
Michael Kaiser-Nyman
Treehouse Guest Teacher

Hey Gary. It looks like you're missing the {{outlet}} helper from your application.hbs. Add that in below </nav> and you should start seeing your templates show up.

Hey,

No, it's there — sorry, I didn't paste the file in full:

<div class="container">

  <nav class="navbar navbar-default" role="navigation">
    <ul class="nav navbar-nav">
      <li>{{#link-to 'posts'}}Posts{{/link-to}}</li>
      <li>{{#link-to 'recent-comments'}}Recent Comments{{/link-to}}</li>
      <li>{{#link-to 'about'}}About{{/link-to}}</li>
      <li>{{#link-to 'contact'}}Contact{{/link-to}}</li>
    </ul>
  </nav>

  {{outlet}}

</div>
Michael Kaiser-Nyman
STAFF
Michael Kaiser-Nyman
Treehouse Guest Teacher

Hm, nothing else jumps out at me right off the bat. One trick you might use for debugging is to just add some debugging text to your post.hbs template, like:

<h1>{{title}}</h1>
<p>{{body}}</p>
testing 123

And see if it shows up.

Yeah, that's the first thing I tried… shows up fine :/

…do you have your source files available to share, so I can check the diffs?

Michael Kaiser-Nyman
STAFF
Michael Kaiser-Nyman
Treehouse Guest Teacher

Okay, great. Try this in your route:

Blogger.PostRoute = Ember.Route.extend({
  model: function(params) {
    debugger;
    return posts.findBy('id', params.post_id);
  }
});

Refresh the page in Chrome with your dev tools open, and it will break execution at the debugger keyword. In the console, make sure that posts, params.post_id, and posts.findBy('id', params.post_id) return what they should.

Michael Kaiser-Nyman
STAFF
Michael Kaiser-Nyman
Treehouse Guest Teacher

I believe if you pop open the Workspace on each video, it will start you out with the finished code from the previous video.

Ah, we're getting somewhere — posts.findBy('id', params.post_id) returns undefined

Michael Kaiser-Nyman
STAFF
Michael Kaiser-Nyman
Treehouse Guest Teacher

Hm hm. But posts and params.post_id return the expected values?

Also, if you can push your code up to Github and put in a link, I can check it out.

Yeah, those two return fine.

Repo here: https://github.com/gazaston/dracula

Bingo! I knew it would be something simple. Thanks for your patience :)