mardi 26 mai 2015

Why isn't this if/else statement executing, javascript?

I made a function with a if-else statement in javascript, but the if-else is being skipped entirely. Looking at similar stack overflow questions the issue was using = instead of == or === in the if statement. however that does not seem to be my problem. So why does execution skip the if-else ?

var crypto = require('crypto');
var Hashtable = require('jshashtable');
var table = new Hashtable();
var session = require('client-sessions');

function getUserID(req, res, callback) {
  if(typeof callback === 'function') {

    var userID;

    /* Lets check if there is a session in place. */
    if(req.session && req.session.userID) {

      /* If there is a session then lets grab the user's userID. */
      userID = req.session.userID;

      callback(null, userID);

    } else {

      /* If there is no session cookie, then this is a brand new visitor/user.
      We will have to create a userId for the visitor, set there session, and store
      the user's state. */

      /* Create the userID. */
      crypto.randomBytes(64, function(error, buf) {
        if(error) {
          console.log('crypto: %s', buf);
          callback(error, null);
        }

        userID = buf.toString('hex');

        /* Lets create a state object. */
        var state = {
          "req": req,
          "res": res
        };

        /* Lets store the 'state' of the user in a hash table. */
        table.put(userID, state);

        /* Now lets pass the userID back to the caller of this function. */
        callback(null, userID);
      });
    }
  /* For some reason code execution skips to here. */
  }
}

Aucun commentaire:

Enregistrer un commentaire