当明确定义globalsvariables时,得到“全局variables未定义”错误

我目前正在重新组织我的web应用程序中的路线(我愚蠢地定义了index.js中的所有路线)的过程中,由于某些原因在这些文件中我有这个莫名其妙的问题:我得到错误说事实上,“globals”variables是未定义的。

这是违规文件之一:

http://pastebin.com/7Q5ExZDa

在第37行,我logging了globals.DB_URL的内容,它存在。 下一行我得到一个错误,全局没有定义。 我究竟做错了什么?

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; } 

更新:第一个问题已经解决了:我没有在utilities.js中导入globals.js,而是试图调用一个需要全局数据来运行的函数。

不幸的是,现在我得到这个错误:

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

这个错误发生在第二个承诺。 我认为这可能与实用程序中的代码有关,特别是identifyUserByToken函数。

 /** * 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; } 

我知道这意味着mongodb是未定义的,但是我将它导入到文件中

  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; 

编辑:解决TypeError

简单的错字。 在实用程序中,我错误地分配了mongoClientvariables

它是如何定义的: var mongoClient = mongodb.mongoClient;

如何定义它: var mongoClient = mongodb.MongoClient;

抱歉! 我的错。

问题是与var mongoClient = mongodb.mongoClient; 应该用大写M:

var mongoClient = mongodb.MongoClient;