"Undefined global variable" error when explicitly defining global variable
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:
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.
The problem is with the var mongoClient = mongodb.mongoClient;
capital M:
var mongoClient = mongodb.MongoClient;