"Undefined global variable" error when explicitly defining global variable


Jimmy Stavros

I'm currently reorganizing the routes in a web application (I stupidly defined all the routes in index.js) and for some reason, in some of those files, I'm having this inexplicable problem: I get the error message, saying that in fact, the "globals" variable is undefined.

Here is one of the files in question:

http://pastebin.com/7Q5ExZDa

On line 37, I log the contents of globals.DB_URL, and it exists. On the next line I get an error that global variable is not 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: Solved the first problem: instead of importing globals.js in Utility.js, I was trying to call a function that needed data from globals to a 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 occurs on the second promise. I think it might have something to do with the code in the utility, 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 that means mongodb is undefined, but I'm importing it in a 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: Fixed TypeError

Simple typo. In the utility I am not assigning the mongoClient variable correctly

Defined by:var mongoClient = mongodb.mongoClient;

How to define it:var mongoClient = mongodb.MongoClient;

I am sorry! My fault.

uglycode

The problem is with the  var mongoClient = mongodb.mongoClient;capital M:

 var mongoClient = mongodb.MongoClient;

Related


undefined global variable

vbujym : package main import ( "crypto/rand" "fmt" ) var a = []byte{0, 0} var b, c []byte func gens(n int) ([]byte, error) { b := make([]byte, n) _, err := rand.Read(b) // Note that err == nil only if we read len(b) bytes. if err !=

Undefined global variable - Python

Milano I have a problem with global variables. return error search = Search.Search(pattern,b) NameError: global name 'b' is not defined But I have defined this global variable. I tried to even put it into the search function. I don't think there is a probl

Undefined global variable - Python

Milano I have a problem with global variables. return error search = Search.Search(pattern,b) NameError: global name 'b' is not defined But I have defined this global variable. I tried to even put it into the search function. I don't think there is a probl

undefined global variable

vbujym : package main import ( "crypto/rand" "fmt" ) var a = []byte{0, 0} var b, c []byte func gens(n int) ([]byte, error) { b := make([]byte, n) _, err := rand.Read(b) // Note that err == nil only if we read len(b) bytes. if err !=

Undefined global variable in Django

Linux Coffee I have a problem with this code in Django, I define two global variables But Django doesn't recognize them my point of view: global phone,rand_num def phone_login(request): if request.method == 'POST': form = PhoneLoginForm(request.POS

Undefined global variable - Python

Milano I have a problem with global variables. return error search = Search.Search(pattern,b) NameError: global name 'b' is not defined But I have defined this global variable. I tried to even put it into the search function. I don't think there is a probl

Undefined global variable in Django

Linux Coffee I have a problem with this code in Django, I define two global variables But Django doesn't recognize them my point of view: global phone,rand_num def phone_login(request): if request.method == 'POST': form = PhoneLoginForm(request.POS

undefined global variable

vbujym : package main import ( "crypto/rand" "fmt" ) var a = []byte{0, 0} var b, c []byte func gens(n int) ([]byte, error) { b := make([]byte, n) _, err := rand.Read(b) // Note that err == nil only if we read len(b) bytes. if err !=

Undefined global variable in Django

Linux Coffee I have a problem with this code in Django, I define two global variables But Django doesn't recognize them my point of view: global phone,rand_num def phone_login(request): if request.method == 'POST': form = PhoneLoginForm(request.POS

Undefined global variable in Django

Linux Coffee I have a problem with this code in Django, I define two global variables But Django doesn't recognize them my point of view: global phone,rand_num def phone_login(request): if request.method == 'POST': form = PhoneLoginForm(request.POS

Undefined global variable - Python

Milano I have a problem with global variables. return error search = Search.Search(pattern,b) NameError: global name 'b' is not defined But I have defined this global variable. I tried to even put it into the search function. I don't think there is a probl

Undefined global variable in Django

Linux Coffee I have a problem with this code in Django, I define two global variables But Django doesn't recognize them my point of view: global phone,rand_num def phone_login(request): if request.method == 'POST': form = PhoneLoginForm(request.POS

Undefined global variable in Django

Linux Coffee I have a problem with this code in Django, I define two global variables But Django doesn't recognize them my point of view: global phone,rand_num def phone_login(request): if request.method == 'POST': form = PhoneLoginForm(request.POS

Undefined global variable in Django

Linux Coffee I have a problem with this code in Django, I define two global variables But Django doesn't recognize them my point of view: global phone,rand_num def phone_login(request): if request.method == 'POST': form = PhoneLoginForm(request.POS

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc

Undefined global variable for vue instance

Toothless Rebel I am defining the Vue.js instance like this app.js: const vm = new Vue({ el: '#app', data: { page: 'welcome' } }); and use Laravel's default webpack script: mix.js('resources/assets/js/app.js', 'public/js') .sass('resourc