MongoDB回调未引发引用错误
Reference error is not thrown from MongoDB callback
简介
所有人都知道,如果我们调用undefined.test
,我们将收到以下错误(对于NodeJS和Javascript都相同):
$ node
> undefined.test
TypeError: Cannot read property 'test' of undefined
at repl:1:11
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
at Interface._line (readline.js:531:8)
at Interface._ttyWrite (readline.js:760:14)
at ReadStream.onkeypress (readline.js:99:10)
at ReadStream.EventEmitter.emit (events.js:98:17)
at emitKey (readline.js:1095:12)
没错!
我是如何发现问题的
一周过去了,我浪费了大约30分钟调试以下问题:一个脚本意外停止,没有抛出任何错误。
我有一个urls
变量,它应该是一个对象:
var urls = settings.urls || {};
然后在接下来的几行中,我需要获得urls
的shop
密钥,它是一个字符串:
var shop = urls.shop || "/";
我开始添加console.log
来查找变量的值:
console.log(urls); // undefined
var shop = urls.shop || "/";
console.log("Passed"); // This isn't run.
我的脚本中的问题是,我正在重新定义一个新的urls
变量,该变量使urls
成为undefined
,但问题是:为什么无法读取未定义的属性"shop"没有出现在这里?因为urls
实际上是undefined
。
我们知道在NodeJS和Javascript中都发生了以下情况:
var a = 10;
foo(function () {
console.log(a); // undefined
var a = 10;
});
function foo(callback) { callback(); }
问题
调试完这个问题后,我发现这个问题来自Mongo:在Mongo回调内部,如果我们调用undefined.something
,我们就不会得到错误。
我创建了一个小脚本来演示这一点:
var mongo = require("mongodb");
// Mongo server
var server = mongo.Server("127.0.0.1", 27017);
var db = new mongo.Db("test", server, { safe: true });
console.log("> START");
// Open database
console.log("Opening database.");
db.open(function(err, db) {
if (err) { return console.log("Cannot open database."); }
// get collection
console.log("No error. Opening collection.");
db.collection("col_one", function(err, collection) {
if(err) { return console.log(err) }
// do something with the collection
console.log("No error. Finding all items from collection.");
collection.find().toArray(function(err, items) {
if(err) { return console.log(err) }
console.log("No error. Items: ", items);
console.log("The following line is: undefined.shop." +
"It will stop the process.");
console.log(undefined.test); // THE ERROR DOES NOT APPEAR!
console.log("> STOP"); // this message doesn't appear.
});
});
});
我的问题是:
- 为什么没有出现错误?原因是什么?(如果能一起调试MongoDB源代码来找到它,那就太好了。)
- 为什么在调用
undefined.something
时进程会停止 - 如何解决这个问题
我创建了一个Github存储库,您可以在这里下载我的小型应用程序来演示这个问题。
有趣:
如果我们添加一个try {} catch (e) {}
语句,我们会发现错误,并且进程会继续显示STOP
消息。
try {
console.log(undefined.test);
} catch (e) {
console.log(e);
}
日志:
> START
Opening database.
No error. Opening collection.
No error. Finding all items from collection.
No error. Items: []
The following line is: undefined.shop. It will stop the process.
[TypeError: Cannot read property 'test' of undefined]
> STOP
查看github.com上的node-mongodb-native
驱动程序问题,您会注意到该问题在1.3.18
版本中得到了解决。但是,我对它进行了测试,但它并没有像预期的那样工作。
相关文章:
- gmaps4rails 2.4.6未捕获引用错误:未定义Gmaps
- Wordpress中的Javascript出现匿名函数/引用错误
- 使用dc.js、d3.js和crossfilter引用错误
- 未捕获引用错误:未定义validateform
- 为什么我会出现此错误"未捕获引用错误:未定义标题;
- $未定义-未捕获引用错误
- Sitecore”;未捕获引用错误:未定义MultilistWithSearch;
- Wordpress:未捕获引用错误:未定义速度
- Node.js引用错误:未定义类名
- 我该如何解决“;未捕获引用错误:谷歌没有定义"?(谷歌地图API)
- 调用setTimeout内部的函数时发生引用错误
- 未捕获的引用错误:未定义<映射键>
- Javascript错误:未捕获引用错误:未定义编辑
- Google可视化addListener调用函数引用错误
- Cordova:未捕获引用错误:chbrody没有使用SQLitePlugin.js定义PhoneGap
- 未捕获引用错误:未定义PrintThisPage
- 引用错误:在尝试使用ajax发送数据时未定义
- 包含的文件中引发引用错误..除非调用文件包含慢速代码
- 在表单提交按钮的点击事件中调用函数时发生引用错误
- 未捕获引用错误:幻灯片未定义