得到"全局未定义“;全局变量定义清楚时出错
Getting "globals is not defined" error when globals variable is clearly defined
我目前正在重新组织我的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;
相关文章:
- 用嵌套函数和默认函数定义函数
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 控制台返回var不是't定义,但它是
- 在自定义mean.io包中使用angular-chart.js作为依赖项
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- jQuery自定义验证比较多个输入的序列
- 查看JS对象的所有键,甚至是getter定义的键
- fetch() 函数未在 Ubuntu Chromium 浏览器上定义
- $window.ga在AngularJS事件中未定义
- 如何在JQuery函数中定义一个值,然后调用另一个函数并使用该值
- 是否有任何方法可以使用jQuery替换在数组中定义值的文本
- 得到"全局未定义“;全局变量定义清楚时出错
- 无法弄清楚为什么本机 Promise 变量未定义
- 如何使 AngularJS 指令在不隔离作用域的情况下清楚地定义其依赖关系
- TypeError: object不是一个函数"在Node中,当对象定义清楚时
- 需要帮助弄清楚为什么这个对象是未定义的
- Javascript错误,函数定义清楚时未定义