JimmyStavros
JimmyStavros

Reputation: 13

Getting "globals is not defined" error when globals variable is clearly defined

I'm currently in the process of reorganizing the routes in my web application (I stupidly defined all the routes in index.js) and for some reason in several of these files I'm having this inexplicable problem: I'm getting errors saying the "globals" variable is undefined when it is, in fact, defined.

This is one of the offending files:

http://pastebin.com/7Q5ExZDa

At line 37 I log the contents of globals.DB_URL, and it exists. The very next line I get an error that globals isn't defined. What am I doing wrong?

mongodb://localhost:27017/[redacted_db_name] // console log output
--- Error: ReferenceError: globals is not defined ---
 Location: function (err){
            utilities.logError(err, arguments.callee.toString());
            res.redirect("/");
            return;
        }

UPDATE: First problem was solved: I wasn't importing globals.js in utilities.js, and was trying to call a function that needed data from globals to function.

Unfortunately, now I get this error:

--- Error: TypeError: Cannot call method 'connect' of undefined ---
 Location: function (err){
            utilities.logError(err, arguments.callee.toString());
            res.redirect("/");
            return;
        }

This error happens at the second promise. I think it may have something to do with the code in utilities, specifically the identifyUserByToken function.

/**
* identifyUserByToken
* Compares a given session token against the session tokens collection
* executes a given callback function if a match is found
* @param {String} userToken The session token to search for
* @param {function(Object, String)} The callback function to be called upon success, failure, or error
*/
function identifyUserByToken(userToken, callback){
    var user_tokens;
    var users
    var promise = new Promise(function(resolve, reject){
        mongoClient.connect(globals.DB_URL)
            .then(function(db){ // Search user_tokens for userToken
                user_tokens = db.collection("user_tokens");
                users = db.collection("users");
                return user_tokens.find({token : userToken}).toArray();
            })
            .then(function(result){ // Search users for the returned userID
                var userID = result[0].userid;
                return users.find({ userid : userID }).toArray();
            })
            .then(function(matchingUsers){ // Pass returned user object to callback
                var user = matchingUsers[0];
                if(callback != undefined) callback(undefined, user);
                resolve(user);
            })
            .catch(function(err){
                if(callback != undefined) callback(err, undefined);
                reject(err);
            });
    });
    return promise;
}

I know this means mongodb is undefined, but I'm importing it in the file

    var globals = require("./globals");

    /* == Third Party Libraries == */
    var chalk = require("chalk"); /* usage: console output coloring */
    var crypto = require("crypto"); /* usage: cryptograpgic primitives (password hashing, etc...) */
    var mongodb = require("mongodb"); /* usage: data storage schema */

    var mongoClient = mongodb.mongoClient;

EDIT: Solved TypeError

Simple typo. In utilities I was assigning the mongoClient variable incorrectly

How it was being defined: var mongoClient = mongodb.mongoClient;

How it needed to be defined: var mongoClient = mongodb.MongoClient;

Sorry! My bad.

Upvotes: 1

Views: 958

Answers (1)

uglycode
uglycode

Reputation: 3082

The issue is with  var mongoClient = mongodb.mongoClient; it should be with a capital M:

 var mongoClient = mongodb.MongoClient;

Upvotes: 1

Related Questions