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 Asynchronous Code in Express Asynchronous Code in Express Refactor Using Async/Await

Anthony Ogounchi
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Anthony Ogounchi
Full Stack JavaScript Techdegree Graduate 14,594 Points

Keep getting a Type error...

I keep getting the above error message. It doesn't seem in my sense that there's an undefined variable or data and I am following exactly the code in the course video. Is anyone has an idea about this?

TypeError: /Users/kolawoleogounchi/OneDrive/programming/treehouse/fsjs/express-async-code/async_express_projectfiles/V4_async_await/views/index.pug:9
    7|   body
    8|     ul
  > 9|       each user in users 
    10|         li(class="user")
    11|           i(class="material-icons") account_box
    12|           h3 #{user.name}

Cannot read property 'length' of undefined
    at eval (eval at wrap (/Users/kolawoleogounchi/OneDrive/programming/treehouse/fsjs/express-async-code/async_express_projectfiles/V4_async_await/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:25:32)
    at eval (eval at wrap (/Users/kolawoleogounchi/OneDrive/programming/treehouse/fsjs/express-async-code/async_express_projectfiles/V4_async_await/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:72:4)
    at template (eval at wrap (/Users/kolawoleogounchi/OneDrive/programming/treehouse/fsjs/express-async-code/async_express_projectfiles/V4_async_await/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:74:92)
    at Object.exports.renderFile (/Users/kolawoleogounchi/OneDrive/programming/treehouse/fsjs/express-async-code/async_express_projectfiles/V4_async_await/node_modules/pug/lib/index.js:427:38)
    at Object.exports.renderFile (/Users/kolawoleogounchi/OneDrive/programming/treehouse/fsjs/express-async-code/async_express_projectfiles/V4_async_await/node_modules/pug/lib/index.js:417:21)
    at View.exports.__express [as engine] (/Users/kolawoleogounchi/OneDrive/programming/treehouse/fsjs/express-async-code/async_express_projectfiles/V4_async_await/node_modules/pug/lib/index.js:464:11)
    at View.render (/Users/kolawoleogounchi/OneDrive/programming/treehouse/fsjs/express-async-code/async_express_projectfiles/V4_async_await/node_modules/express/lib/view.js:135:8)
    at tryRender (/Users/kolawoleogounchi/OneDrive/programming/treehouse/fsjs/express-async-code/async_express_projectfiles/V4_async_await/node_modules/express/lib/application.js:640:10)
    at Function.render (/Users/kolawoleogounchi/OneDrive/programming/treehouse/fsjs/express-async-code/async_express_projectfiles/V4_async_await/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/Users/kolawoleogounchi/OneDrive/programming/treehouse/fsjs/express-async-code/async_express_projectfiles/V4_async_await/node_modules/express/lib/response.js:1008:7)
Cas F
seal-mask
.a{fill-rule:evenodd;}techdegree
Cas F
Full Stack JavaScript Techdegree Student 21,170 Points

Hello, I'm not sure why TreeHouse included me as a potential person to answer this as I'm not this far yet in the JavaScript modules, but research would indicate that somewhere in your code might somethings not referencing the Users object correctly.

https://github.com/apostrophecms/apostrophe-pieces-submit-widgets/issues/11 <--- a similar situation solved by a spelling error.

I would recommend reviewing your code after stepping away from it for a bit and make sure your aren't missing any letters, syntax, etc.

Hope this helps!

Cas

1 Answer

James Crosslin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James Crosslin
Full Stack JavaScript Techdegree Graduate 16,882 Points

For future people encountering this problem, it is definitely from an incorrect assignment of users in your app.js file. Make sure your res.render method looks like this:

    res.render("index", { title: "Users", users: users.users })

Treasure makes a point to explain why this is necessary in the previous video. If you look at the data.json file, you'll see that each of the users we want to see exist in an array of values with the "users" key. Because the JSON object itself has no name, when we parse it, we define the data with the variable name users. Thus, the entirety of this JSON file is saved by us as users.

{
  "users": [
    {
      "id": 1,
      "name": "Dean Winchester",
      "username": "Impala67",
      "bio": "Renown ghost hunter"
    },
    {
      "id": 2,
      "name": "Charles",
      "username": "silverpothos4ever",
      "bio": "A majestic houseplant who lives in the Treehouse office"
    },
    {
      "id": 3,
      "name": "Bugs Bunny",
      "username": "bugs1234",
      "bio": "A famous carrot enthusiast"
    },
    {
      "id": 4,
      "name": "Squash",
      "username": "kingcharlesdog",
      "bio": "The cutest office dog at Treehouse"
    }
  ]
}

Now you can see that within our object is a property called users. In order to assign this to a parameter, we use dot notation:

users(parameter): users(full parsed JSON object).users(property within the JSON object)

I hope that eased some confusion for the next person looking on!