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 Models Loading Arrays of JavaScript objects

Ernest Guaimano
Ernest Guaimano
14,928 Points

I am having a lot of issues with the update to ember. Is the only necessary change with the new handlebars syntax?

In posts.hbs I have

<ul>
    {{#each post in posts}}
        <li>{{post.title}}</li>
    {{/each}}
</ul>

The app loads without issue but no data from the array is printed. I have the same issue with the recent comments. My guess is that there is an issue with my store.js file. Please find below. Any guesses?

var posts = [
  {
    id: '1',
    title: "I love blood",
    body: "I awoke in my own bed. If it be that I had not dreamt, the Count must have carried me here. I tried to satisfy myself on the subject, but could not arrive at any unquestionable result. To be sure, there were certain small evidences, such as that my clothes were folded and laid by in a manner which was not my habit. My watch was still unwound, and I am rigorously accustomed to wind it the last thing before going to bed, and many such details. But these things are no proof, for they may have been evidences that my mind was not as usual, and, for some cause or another, I had certainly been much upset.I must watch for proof. Of one thing I am glad.If it was that the Count carried me here and undressed me, he must have been hurried in his task, for my pockets are intact. I am sure this diary would have been a mystery to him which he would not have brooked.He would have taken or destroyed it. As I look round this room, although it has been to me so full of fear, it is now a sort of sanctuary, for nothing can be more dreadful than those awful women, who were, who are, waiting to suck my blood."
  },
  {
    id: '2',
    title: "The Count",
    body: "I have given the letters. I threw them through the bars of my window with a gold piece, and made what signs I could to have them posted. The man who took them pressed them to his heart and bowed, and then put them in his cap. I could do no more. I stole back to the study, and began to read. As the Count did not come in, I have written here."
  },
  {
    id: '3',
    title: "Thievery",
    body: "Every scrap of paper was gone, and with it all my notes, my memoranda, relating to railways and travel, my letter of credit, in fact all that might be useful to me were I once outside the castle. I sat and pondered awhile, and then some thought occurred to me, and I made search of my portmanteau and in the wardrobe where I had placed my clothes."
  }
];

var comments = [
  {
    id: '1',
    postId: '3',
    text: 'What a shame!'
  },
  {
    id: '2',
    postId: '3',
    text: 'I am so sorry. How are you feeling now?'
  },
  {
    id: '3',
    postId: '1',
    text: 'I love blood too!'
  },
  {
    id: '4',
    postId: '1',
    text: 'Eeew, blood is gross.'
  },
  {
    id: '5',
    postId: '2',
    text: 'This all sounds so mysterious.'
  }
];

2 Answers

Bharani M
Bharani M
756 Points

I ran into the exact same issue yesterday. I tried replacing posts inside the #each block to model and it seems to work, but I am not sure if this is the right way to do it.

<ul>
    {{#each post in model }}
        <li>
            {{#link-to 'post' post.id}}{{ post.title }}{{/link-to}}
        </li>
    {{/each}}
</ul>
Ernest Guaimano
Ernest Guaimano
14,928 Points

Thanks for the suggestion. This works for me as well but I am not entirely sure why it must be model. I did not think that we defined this anywhere in the application.