Debugging, debugging, debugging

So the last week of work on the project has been very telling experience for me developing with node/express/mongodb/backbone. These technologies are so close in proximity since they are all javascript, but all have their own languages and syntax for doing several things. It should go without saying that the errors I made were still mostly human error on my part, but the error messages lead me down the wrong path. The many working files I was tearing through caused me to look at files where the origin of the problem did not reside. So here are the issues I ran into the most:

  1. incorrect syntax of my mongoose schema
  2. incorrect naming of object references in backbone
  3. incorrect updating of post command to write to the database

The first issue of incorrect syntax was probably the easiest to solve, and did not take long to look up in my reference books or online in the mongoose documentation. I pretty much had it figured out the first night, and working inside Chrome and the control panel gave me easy clues to solve the problems at hand.

The second issue of object references took a little longer to find, but it circulated around the following line of code:

account.status.push(status);
account.activity.push(status);

These lines of code take the status (which I will rename to recipe) and activity and push new statuses into them, so that they can display. However in trying to rename to recipe the first time I mistakenly named one reference with the word “recipes” which did take a while to track down. One reason for the inability to track this down was the error message kept leading me to this this piece of code:

$.post('/accounts/me/status', {
        status: statusText
      }, function(data) {
        statusCollection.add(new Status({status:statusText}));      
});

I have condensed this code to what was originally in the book, however I attempted to add in my attributes, which ended up being also my final problem – however this current issue kept circling me around this code instead of the latter piece of code, which ended up being the main problem – a typo.

For my final problem, I was hoping to achieve the following object when things were being recalled from the database:

onStatusCollectionReset: [{"fn":"recipe1","instructions":"instruction1","yield":"4",
"duration":"10mins","photo":"./","video":"./","summary":"summary1",
"published":"Better Homes","tag":"numerous|tags",
"_id":"516c8929f7920cd02400000a","nutrition":[],"author":[],"secretIngredients":[],
"ingredients":[]}]

Unfortunately, my code was only returning the empty arrays for those items in which I wanted multiple strings attached to it.

onStatusCollectionReset: [{"_id":"516c8929f7920cd02400000a","nutrition":[],
"author":[],"secretIngredients":[], "ingredients":[]}]

So after much mashing of teeth, I did manage to corner the problem. Basically, it was clear that the database was not publishing my items, because no items were getting retrieved. And it was saying there was something wrong with the post, which lead me to the last code sample, as I previously mentioned. But what the error did not tell me was to look at the post inside the app.js file that held the entire project (because the error occurred when the data was being retrieved, not when the data was lacking its writing to the database). In any case, here is the code that needed to be changed:

app.post('/accounts/:id/status', function(req, res) {
  var accountId = req.params.id == 'me'
                     ? req.session.accountId
                     : req.params.id;

  models.Account.findById(accountId, function(account) {
    status = {
      name: account.name,
      fn: req.param('fn', ''),
      instructions: req.param('instructions', ''),
      yield: req.param('yield', ''),
      duration: req.param('duration', ''),
      photo: req.param('photo', ''),
      video: req.param('video', ''),
      summary: req.param('summary', ''),
      published: req.param('published', ''),
      tag: req.param('tag', ""),
    };

The original code only had

app.post('/accounts/:id/status', function(req, res) {
  var accountId = req.params.id == 'me'
                     ? req.session.accountId
                     : req.params.id;

  models.Account.findById(accountId, function(account) {
    status = {
      name: account.name,
      status: req.param('status', ''),

    };

Because the object and the attribute were both named status, it did make it difficult to figure out where all of the references were pointing to, but in the end it did get figured out.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>