得到"全局未定义“;全局变量定义清楚时出错

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

本文关键字:清楚 定义 出错 全局变量 未定义 quot 全局 得到      更新时间:2023-09-26

我目前正在重新组织我的web应用程序中的路由(我愚蠢地在index.js中定义了所有路由),出于某种原因,在其中的几个文件中,我遇到了一个无法解释的问题:我收到错误,说"globals"变量在定义时是未定义的。

这是一个有问题的文件:

http://pastebin.com/7Q5ExZDa

在第37行,我记录了全局变量的内容。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,而是试图调用一个需要从globals到函数的数据的函数。

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

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

编辑:已解决类型错误

简单的打字错误。在实用程序中,我错误地分配了mongoClient变量

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

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

对不起!我的坏。

问题是 var mongoClient = mongodb.mongoClient;,它应该是大写M:

 var mongoClient = mongodb.MongoClient;