Undefined Is the New Black - tdkehoe/blog GitHub Wiki

Callback functions are asynchronous. Asynchronous code is executed after synchronous code. Synchronous code that is below asynchronous code will appear to execute out of order. Watch out for synchronous code that ends a thread, e.g, return or res.end

Highlight synchronous and asynchronous commands.

#Unpack Abstractions

See wiki page "JavaScript Arrays to Objects".

Unpack abstractions; abstract repetitive code.

Chrome Dev Tools unpacks abstractions. If you open your website in Chrome, then open Dev Tools, then look at the code in Sources, you'll see your unabstracted code. E.g., a Mustache command {{#movies}}...{{/movies} to iterate through a MongoDB collection of objects is replaced by the iterated results.

#JavaScript Error Messages

Cannot read property 'fn' of undefined. No route found. Object on left of dot notation.

#Assume there's more than one bug

I couldn't get an edit page to update MongoDB. I checked the syntax for updates and saw that it takes two objects, one to identify the database record to be updated and a second for the updated record. I only had the second argument so I put in the first object. It didn't work. I then spent more than an hour formatting the object in different ways, without success. Then I noticed that I also had the route wrong. An edit page takes two HTTP requests, a GET to serve the edit page, and then a POST to send the user's edits to the database. The GET request was working so I thought the route was correct, but I didn't see that the POST route was incorrect. I fixed that and the page worked.

The lesson is that if you get stuck on a bug, look if there are any other bugs.

#Is It Plugged In? If your app requires a server and MongoDB running, keep terminal windows open for both server and MongoDB to see if one stops running.

#ENOENT

No route found.

#Error: listen EADDRINUSE

Server process already running.

"start": "node --harmony app.js",
"dev": "nodemon --harmony app.js"

ps | grep node

Process ID is the first number in the response.

kill <processId>

ps -ax | grep node

killall -9 node

#Not 12 0r 24 Hex

MongoDB is sending this error message. You're sending something to MongoDB other than database object, possibly a route instead of having the route action send the database object.

#Can't set headers after they are sent

Trying to render a page after it was rendered?

#Undefined Is Not a Function

Figure out what variable the undefined method is attached to, and look if the variable is defined twice. If the bug is intermittent, working sometimes and sometimes not working, the definition is changing as different code blocks are executed.

⚠️ **GitHub.com Fallback** ⚠️