MongoDB回调未引发参考错误 [英] Reference error is not thrown from MongoDB callback
问题描述
所有人都知道,如果我们调用undefined.test
,将会收到以下错误(NodeJS和Javascript相同)
All people know that if we call undefined.test
we will receive the following error (same for both: NodeJS and 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分钟的时间:脚本意外停止并且未引发任何错误.
Passed week I wasted about 30 minutes in debugging the following problem: A script was stopping accidentally and no error was thrown.
我有一个应该作为对象的urls
变量:
I had the urls
variable that was supposed to be an object:
var urls = settings.urls || {};
然后在接下来的几行中,我需要获取作为字符串的urls
的shop
键:
Then in next lines I needed to get shop
key of urls
that was a string:
var shop = urls.shop || "/";
我开始添加console.log
来查找变量的值:
I started adding console.log
to find the values of variables:
console.log(urls); // undefined
var shop = urls.shop || "/";
console.log("Passed"); // This isn't run.
脚本中的问题是我正在重新定义正在生成urls
undefined
的新 urls
变量,但是问题是:为什么 无法显示读取的未定义属性商店" 没有出现在这里?因为urls
确实是undefined
.
The problem in my script was that I was redefining a new urls
variable that was making the urls
undefined
, but the question is: why cannot read property "shop" of undefined didn't appear here? Because urls
was really undefined
.
我们知道,NodeJS和Javascript都在发生以下情况:
We know that the following is happening in both: NodeJS and Javascript:
var a = 10;
foo(function () {
console.log(a); // undefined
var a = 10;
});
function foo(callback) { callback(); }
问题
在调试问题后,我发现此问题来自Mongo:在Mongo回调中,如果我们调用undefined.something
,我们不要会收到错误.
The question
After debugging the problem I found that this problem comes from Mongo: inside of Mongo callbacks if we call undefined.something
we DON'T get the error.
我创建了一个小脚本来演示这一点:
I've created a small script that demonstrates this:
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
时进程停止? - 如何解决?
- Why the error doesn't appear? Which is the reason? (It would be great to debug together the MongoDB source code to find it.)
- Why the process is stopped when calling
undefined.something
? - How can be this solved?
我已经创建了一个 Github存储库 ,您可以在其中下载我的小文件演示问题的应用程序.
I've created a Github repository where you can download my small application that demonstrates the issue.
有趣:
如果添加try {} catch (e) {}
语句,我们会发现错误,并且过程将继续显示STOP
消息.
If we add a try {} catch (e) {}
statement we find the error and the process continue showing the STOP
message.
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
版本中该问题已解决.但是,我对其进行了测试,但无法正常工作.
Looking on github.com at node-mongodb-native
driver issues, you will notice that issue is solved in 1.3.18
version. But, I tested it and it does not work as expected.
这篇关于MongoDB回调未引发参考错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!