WebFund 2015W: Assignment 8

From Soma-notes

This assignment has 10 points. For five of the following eight error messages/conditions, please explain what small code change to ajax-notes (from Assignment 7) or its running environment would produce the error (1 point) and briefly explain why this code change produces this error (1 point). You may answer one additional question for extra credit, allowing you to get a maximum score of 12 (out of 10) on this assignment.

Submit your answers on cuLearn in a text or PDF file by 10 AM on Wednesday, March 25, 2015. No other formats will be accepted. No late assignments will be accepted!

Error 1

The following message was printed at the terminal prompt after running bin/www:

/home/soma/Documents/Class/WebFund/2015W/ajax-notes-broken/node_modules/mongodb/lib/mongodb/mongo_client.js:409
          throw err
                ^
Error: failed to connect to [localhost:27017]
    at null.<anonymous> (/home/soma/Documents/Class/WebFund/2015W/ajax-notes-broken/node_modules/mongodb/lib/mongodb/connection/server.js:555:74)
    at emit (events.js:106:17)
    at null.<anonymous> (/home/soma/Documents/Class/WebFund/2015W/ajax-notes-broken/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:156:15)
    at emit (events.js:98:17)
    at Socket.<anonymous> (/home/soma/Documents/Class/WebFund/2015W/ajax-notes-broken/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10)
    at Socket.emit (events.js:95:17)
    at net.js:440:14
    at process._tickCallback (node.js:419:13)

Error 2

You are logged in and you try to go to http://localhost:3000/ (the top-level page). Rather than redirecting to /notes the load just hangs.

Error 3

The notes listing screen only says "Loading Notes...". When you look at the network tab in the browser developer tools you notice a GET for /getNotes has returned a 500 status code.

Error 4

In the notes listing you see only one note listed with the title "Not Logged In" and contents of "Nobody seems to be logged in!". The top of the page says the current username and the logout button still works normally.

Error 5

Clicking the "New Note" button seems to do nothing. However, a POST request to /newNote has a response code of 200 and, when refreshed, there is a new, "Untitled" note added to the list.

Error 6

On the /notes page, when logged in as Alice, you only see "Alice account", "Welcome Alice", and the logout button (which works fine). In the JavaScript console in the browser you see a message "ReferenceError: $ is not defined".

Error 7

On the /notes page, when logged in as Alice, you only see "Alice account", "Welcome Alice", and the logout button (which works fine). In the JavaScript console in the browser you see no new error messages. In the network view you see four 200-code GET requests for notes, style.css, jquery-1.1.12.js and notes.js.

Error 8

The "Refresh Notes" button doesn't seem to do anything. The rest of the application seems to work fine.


Solutions

  1. This error happens because MongoDB is not running (e.g., it has crashed or somebody ran service mongodb stop). The application, via mc.connect(), attempts to connect to mongodb on localhost at port 27017 and gets a connection refused. This generates an error that is passed on to the anonymous callback for mc.connect(), which then calls "throw err". Because we don't have a catch statement to catch this error it causes the application to terminate with the error given above.
  2. This error means that the handler function for / is not properly redirecting to /notes when the user is logged in. One code change that could produce this effect is the omission of the res.redirect("/notes") call in the function passed to router.get() for /.
  3. The 500 return status code for GET /getNotes means that the handler is generating some sort of error that is being caught by the Express framework. (That is why the error is being returned to the client rather than crashing the application.) One of the simplest kinds of errors that could generate this sort of problem is an unknown identifier. For example, if "username" was instead entered as "Username" (in the if (username) test) we could get a 500 error. Or, if the declaration of username was left out at the top of the function we would get a 500 error.
  4. The note being displayed is the one that is supposed to be sent when no user is logged in; however, the user is, indeed, logged in as evidenced by the working logout button. There is thus a logic error in the /getNotes callback. One way to generate this type of error would be to invert the if test of username (that is used to determine whether someone is logged in in the current session), e.g. by replacing "if (username)" with "if (!username)".
  5. A new note is being added, so everything is working server side. The client (notes.js) is making the appropriate POST request because we see the note added. What we aren't seeing is the edit interface. The callback editNewNote() is responsible for initiating the call to editNote() which puts up the edit interface. Thus one way to generate the observed error would be to delete the call to editNote() in editNewNote().
  6. If $ is not defined, then the $() enclosing all of the code in notes.js won't be defined and so none of the code in notes.js will be run, leading to the empty interface. $ is used in our code to access jQuery, so the error means that jQuery is not being loaded properly. Thus a simple way to produce this error would be to delete the script line in layout.jade that loads jQuery.